2021 Winter Holiday Algorithm Basic Training Camp 6 B. Coefficient (Lucas Theorem)

Title:

Insert picture description here
Insert picture description here

solution:

f (x)% 3 = (x 2 + x + 1) n = ((x − 1) 2 + 3 x) n Since 3 x% 3 = 0, f (x) = (x − 1) 2 n The coefficient of the k-th term is C (2 n, k) ∗ (− 1) 2 n − k Because it can be% 3, C (2 n, k) can be calculated by Lucas' theorem. f(x)\% 3=(x^2+x+1)^n \\ =((x-1)^2+3x)^n \\ Since 3x\%3=0,\\ Therefore f(x)=(x- 1)^{2n} \\ The coefficient of the k-th term is C(2n,k)*(-1)^(2n-k)\\ because \%3, C(2n,k) can use Lucas theorem Just do the math. f(x)%3=(x2+x+1)n=((x1)2+3 x )nOf at . 3 X % . 3=0,Because this f ( the X- )=(x1)2 nThe first k terms of line number is C ( 2 n- ,k)(1)2 n - kDue to be in % 3 ,C(2n,k ) with Lu card Sri Lanka given reason considered a case on the line up .

code:

#include <bits/stdc++.h>
#define int long long
using namespace std;
const int maxm=3e5+5;
const int mod=3;
int c[3][3];
int n,k;
void init(){
    
    
    for(int i=0;i<3;i++){
    
    
        c[i][0]=1;
        for(int j=1;j<=i;j++){
    
    
            c[i][j]=(c[i-1][j]+c[i-1][j-1])%mod;
        }
    }
}
int C(int n,int m){
    
    
    if(m<0||m>n)return 0;
    return c[n][m];
}
int lucas(int n,int m){
    
    
    if(m==0)return 1;
    return C(n%mod,m%mod)*lucas(n/mod,m/mod)%mod;
}
void solve(){
    
    
    cin>>n>>k;
    int ans=lucas(2*n,k);
    if((2*n-k)&1)ans=(-ans+mod)%mod;
    cout<<ans<<endl;
}
signed main(){
    
    
    ios::sync_with_stdio(0);
    init();
    int T;cin>>T;
    while(T--){
    
    
        solve();
    }
    return 0;
}

Guess you like

Origin blog.csdn.net/weixin_44178736/article/details/114100403