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

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 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)中$ 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 2cos2x1
所以 f ( x ) = 2 x 2 − 1 f(x)=2x^2-1 f(x)=2x21

思路

用以表示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)2nmk!(nk)!!n(n+k2)!!
其中!!表示双阶乘如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) (nk)!!(n+k2)!!=(nk+2)(nk+4)(n+k2)
k!还要用逆元处理一下,用费马小定理求逆元
然后我们观察一下前几项n的式子会发现
c o s 2 x = 2 c o s 2 x − 1 cos2x=2cos^2x-1 cos2x=2cos2x1
c o s 3 x = 4 c o s 3 x − 3 c o s x cos3x=4cos^3x-3cosx cos3x=4cos3x3cosx
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=8cos4x8cos2x+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=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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值