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

本文介绍了一种利用切比雪夫多项式计算特定系数的方法,并提供了一个C++程序实现,该程序能够计算给定n和m情况下f(cos(x))=cos(nx)中x^m的系数,所有计算结果取模998244353。

f(cos(x))=cos(nx) holds for all xxx.

Given two integers nnn and mmm, you need to calculate the coefficient of xmx^mxm 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 ^ 41n109,0m104.

Output Format

Output the answer in a single line for each test case.

样例输入
2 0
2 1
2 2
样例输出
998244352
0
2
题目来源

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


解题思路:查了一下数列大全,发现是切比雪夫多项式,然后套公式,就出来了。附上队友代码。



#include<stdio.h>
#include<string.h>
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long long LL;

const int MOD = 998244353;

/*pwr_mod*/ inline int pwr_mod(ll x, ll y, ll p) {   ll res = 1;    x = x % p;   while (y > 0) {   if (y & 1) res = (res*x) % p; y = y>>1;  x = (x*x) % p;  }  return res; }

/*modulo inverse _p*/ll mod_inv_p(ll n, ll m){ return pwr_mod(n, m-2, m); }


ll n,m;
ll res;

ll regular(ll x){
    if(x>=0)return x%MOD;
    else return (x+MOD)%MOD;
}
inline ll neg1pow(ll x){
    if(x&1)return -1; else return 1;
}

LL Comb(LL a, LL b, LL p) {
    if(a < b)   return 0;
    if(a == b)  return 1;
    if(b > a - b)   b = a - b;

    LL ans = 1, ca = 1, cb = 1;
    for(LL i = 0; i < b; ++i) {
        ca = (ca * (a - i))%p;
        cb = (cb * (b - i))%p;
    }
    ans = (ca*pwr_mod(cb, p - 2, p)) % p;
    return ans;
}

LL Lucas(int n, int m, int p) {
     LL ans = 1;

     while(n&&m&&ans) {
        ans = (ans*Comb(n%p, m%p, p)) % p;
        n /= p;
        m /= p;
     }
     return ans;
}

ll chebyshev(ll n, ll m){
    if((n+m)%2!=0 || n<m)return 0;
    else if(m==0){
        return neg1pow(n/2);
    }
    else{
        ll part1 = neg1pow((n+m)/2 + m);
        ll part2 = pwr_mod(2,m-1,MOD);
        ll part3 = n%MOD;
//         printf("%lld:%lld:\n",(n+m)/2 - 1, m - 1);
        ll part4 = Lucas((n+m)/2 - 1, m - 1,MOD);
        ll part5 = mod_inv_p(m,MOD);
//         printf("%lld,%lld,%lld,%lld,%lld,\n",part1,part2,part3,part4,part5);
        ll part6 = (part1*part2)%MOD;
        ll part7 = (part6*part3)%MOD;
        ll part8 = (part7*part4)%MOD;
        ll part9 = (part8*part5)%MOD;
        return     part9;
    }
}

int main(){
    while(~scanf("%lld%lld",&n,&m)){
        printf("%lld\n",regular(chebyshev(n,m)));
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值