2017 ACM-ICPC 亚洲区(西安赛区)网络赛 Trig Function

来源:互联网 发布:mac好玩的联机游戏 编辑:程序博客网 时间:2024/06/05 08:53

2017 ACM-ICPC 亚洲区(西安赛区)网络赛 Trig Function

f(cos(x))=cos(n∗x) holds for all xxx.

Given two integers nnn and mmm, you need to calculate the coefficient of xmx^mx​m​​ in f(x)f(x)f(x), modulo 998244353998244353998244353.
Input Format

Multiple test cases (no more than 100100100).

Each test case contains one line consisting of two integers nnn and mmm.

1≤n≤109,0≤m≤1041 \le n \le 10^9,0 \le m \le 10 ^ 41≤n≤10​9​​,0≤m≤10​4​​.
Output Format

Output the answer in a single line for each test case.
样例输入

2 0
2 1
2 2

样例输出

998244352
0
2

切比雪夫多项式

这里写图片描述

注意:!!是双阶乘

#include <cstdio>#include <cstring>#include <algorithm>#include <iostream>using namespace std;typedef long long ll;#define mod 998244353ll q[100010];ll n,m;void init(){    q[0]=1;    for (int i=1;i<100010;i++){        q[i]=q[i-1]*i%mod;    }}ll qpow(ll n,ll m){    ll res=1;    while(m){        if (m&1)            res=res*n%mod;            n=n*n%mod;            m>>=1;    }    return res;}int main(){    init();    while(~scanf("%lld%lld",&n,&m)){        if (n<m){            printf("0\n");            continue;        }        if (n%2&&m%2==0){            printf("0\n");            continue;        }        if (n%2==0&&m%2){            printf("0\n");            continue;        }        ll fz=n%mod;        if (m>=1){            for (int i=n-m+1;i<=n+m-1;i++){                if (i%2==(n+m-2)%2)                fz=fz*i%mod;            }            ll x=fz*qpow(q[m],mod-2)%mod;            if ((n-m)/2%2)                x=-x;            printf("%lld\n",(x+mod)%mod);        }        else {            ll t =1;            for (int i=m+n-1;i<=n-m;i++){                if (i%2==(n+m-2)%2)                t=t*i%mod;            }            ll x =fz*qpow(q[m],mod-2)%mod*qpow(t,mod-2)%mod;            if ((n-m)/2%2)                x=-x;                printf("%lld\n",(x+mod)%mod);        }    }}
阅读全文
0 0
原创粉丝点击