您的位置:首页 > 理论基础 > 计算机网络

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

2017-09-16 16:58 441 查看
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.

1≤n≤109,0≤m≤104

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 = 2cos2x−1

所以 f(x)=2x2−1

思路

用以表示cosnx的关于cosx的多项式的通项公式

得到通项的系数为

(−1)n−m2n(n+k−2)!!k!(n−k)!!

其中!!表示双阶乘如6!!=6x4x2=48,5!!=5x3x1=15(且0!!=1)

那么我们稍微化简一下

易知(n+k-2)与(n-k)奇偶性相同那么

(n+k−2)!!(n−k)!!=(n−k+2)(n−k+4)⋯(n+k−2)

k!还要用逆元处理一下,用费马小定理求逆元

然后我们观察一下前几项n的式子会发现

cos2x=2cos2x−1

cos3x=4cos3x−3cosx

cos4x=8cos4x−8cos2x+1

cos5x=16cos5x−20cos3x+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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐