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

来源:互联网 发布:java程序员职业规划 编辑:程序博客网 时间:2024/06/05 01:08

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

Given two integers nn and mm, you need to calculate the coefficient ofxm
​​ 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

题意

给你n问你原函数f(x)中xm这一项的系数
如n=2
f(cosx)=cos2x = 2cos2x1
所以 f(x)=2x21

思路

用以表示cosnx的关于cosx的多项式的通项公式
得到通项的系数为
(1)nm2n(n+k2)!!k!(nk)!!
其中!!表示双阶乘如6!!=6x4x2=48,5!!=5x3x1=15(且0!!=1)
那么我们稍微化简一下
易知(n+k-2)与(n-k)奇偶性相同那么
(n+k2)!!(nk)!!=(nk+2)(nk+4)(n+k2)
k!还要用逆元处理一下,用费马小定理求逆元
然后我们观察一下前几项n的式子会发现
cos2x=2cos2x1
cos3x=4cos3x3cosx
cos4x=8cos4x8cos2x+1
cos5x=16cos5x20cos3x+5cosx
会发现但n和m的奇偶性一致时才会有系数否则为0

#include <iostream>#include <cstdio>#include <cstring>#include <math.h>using namespace std;const long long mod=998244353;long long quickmmod(long long a,long long b){    long long ans=1;    a%=mod;    while(b>0)    {        if(b%2==1)            ans=ans*a%mod;        b/=2;        a=a*a%mod;    }    return ans;}int main(){    long long n,m;    while(scanf("%lld%lld",&n,&m)!=EOF)    {        if(m>n)            printf("0\n");        else if((n&1)!=(m&1))            printf("0\n");        else        {            long long flag=(n-m)/2&1?-1:1;            long long ans=1;            for(int i=1;i<=m;i++)                ans=(ans*i)%mod;            ans=quickmmod(ans,mod-2);            for(int i=n-m+2;i<=n+m-2;i+=2)                ans=(ans*i)%mod;            ans=ans*n%mod;            ans*=flag;            printf("%lld\n",(ans+mod)%mod);        }    }    return 0;}
阅读全文
0 0
原创粉丝点击