2017 ACM-ICPC 亞洲區(西安賽區)網絡賽 F.Trig Function(論文+組合數)

傳送門

f(cos(x))=cos(nx) holds for all x .

Given two integers n and m , you need to calculate the coefficient of xm in f(x) , modulo 998244353 .

Input Format

Multiple test cases (no more than 100 ).

Each test case contains one line consisting of two integers n and m .

1n109,0m104 .

Output Format

Output the answer in a single line for each test case.




樣例輸入


2 0 
2 1
2 2



樣例輸出


998244352 
0
2


題目大意:

給出f(cos(x))=cos(nx) ,求xm 前面的係數,其實就是求 cos(x)m 前面的係數。

解題思路:

首先給出論文:傳送門
然後就根據論文進行計算就OK了,需要特判兩個點。
給出組合數公式:
這裏寫圖片描述
1) m==0 的時候
2) m==n 的時候

代碼:

#include <iostream>
#include <string.h>
#include <string>
#include <algorithm>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <map>
using namespace std;
typedef long long LL;
const int MAXN = 1e4+5;
const double PI = acos(-1);
const double eps = 1e-8;
const LL MOD = 998244353;
LL Pow(LL a, LL b){
    LL ans = 1;
    while(b){
        if(b & 1) ans = ans * a % MOD;
        b>>=1;
        a = a * a % MOD;
    }
    return ans;
}

LL Inv[MAXN];
void Init(){
    Inv[0] = Inv[1] = 1;
    for(int i=2; i<MAXN; i++) Inv[i] = (MOD - MOD / i) * Inv[MOD % i] % MOD;
    for(int i=2; i<MAXN; i++) Inv[i] = Inv[i]*Inv[i-1]%MOD;
}
int main(){
    //freopen("C:/Users/yaonie/Desktop/in.txt", "r", stdin);
    //freopen("C:/Users/yaonie/Desktop/out.txt", "w", stdout);
    Init();
    LL n, m;
    while(~scanf("%lld%lld", &n, &m)){
        if(((n&1)&&!(m&1)) || (!(n&1)&&(m&1))){
            puts("0");
            continue;
        }
        LL k = (n-m)/2;
        if(m == 0){
            if(k & 1) puts("998244352");
            else puts("1");
            continue;
        }
        if(n == m){
            printf("%lld\n",Pow(2LL, n-1));
            continue;
        }
        LL t1 = min(k, n-2*k), t2 = min(k-1, n-2*k);
        LL ans1 = Inv[t1], ans2 = Inv[t2];
        for(LL i=1; i<=t1; i++){
            LL tmp = (n-k-i+1)%MOD;
            ans1 = ans1*tmp%MOD;
        }
        for(LL i=1; i<=t2; i++){
            LL tmp = (n-k-i)%MOD;
            ans2 = ans2*tmp%MOD;
        }
        LL ans = (ans1 + ans2) % MOD;
        if(k & 1) ans=-ans;
        ans = (ans+MOD)%MOD;
        ans = ans*Pow(2LL, n-1-2*k)%MOD;
        printf("%lld\n",ans);
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章