f(cos(x))=cos(n∗x) holds for all x.
Given two integers nn and mm, you need to calculate the coefficient of$ x^m$
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
≤
1
0
9
,
0
≤
m
≤
1
0
4
1 \le n \le 10^9,0 \le m \le 10 ^ 4
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)中$ x^m$这一项的系数
如n=2
f
(
c
o
s
x
)
=
c
o
s
2
x
f(cosx)=cos2x
f(cosx)=cos2x =
2
c
o
s
2
x
−
1
2cos^{2}x-1
2cos2x−1
所以
f
(
x
)
=
2
x
2
−
1
f(x)=2x^2-1
f(x)=2x2−1
思路
用以表示cosnx的关于cosx的多项式的通项公式
得到通项的系数为
(
−
1
)
n
−
m
2
n
(
n
+
k
−
2
)
!
!
k
!
(
n
−
k
)
!
!
\left ( -1 \right )^{\frac{n-m}{2}}\frac{n(n+k-2)!!}{k!(n-k)!!}
(−1)2n−mk!(n−k)!!n(n+k−2)!!
其中!!表示双阶乘如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
)
\frac{(n+k-2)!!}{(n-k)!!}=(n-k+2)(n-k+4) \cdots (n+k-2)
(n−k)!!(n+k−2)!!=(n−k+2)(n−k+4)⋯(n+k−2)
k!还要用逆元处理一下,用费马小定理求逆元
然后我们观察一下前几项n的式子会发现
c
o
s
2
x
=
2
c
o
s
2
x
−
1
cos2x=2cos^2x-1
cos2x=2cos2x−1
c
o
s
3
x
=
4
c
o
s
3
x
−
3
c
o
s
x
cos3x=4cos^3x-3cosx
cos3x=4cos3x−3cosx
c
o
s
4
x
=
8
c
o
s
4
x
−
8
c
o
s
2
x
+
1
cos4x=8cos^4x-8cos^2x+1
cos4x=8cos4x−8cos2x+1
c
o
s
5
x
=
16
c
o
s
5
x
−
20
c
o
s
3
x
+
5
c
o
s
x
cos5x=16cos^5x-20cos^3x+5cosx
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;
}