60pts#RE求解
查看原帖
60pts#RE求解
586689
K1ondra楼主2024/7/11 10:12
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN=1e8+100;
int n,p,k,r;
ll fact[MAXN]; 
ll ifact[MAXN];

ll modPow(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;
}

void sb(int n, int p){
    fact[0]=1;
    for(int i=1;i<=n;++i){
        fact[i]=(fact[i-1]*i)%p;
    }
    ifact[n]=modPow(fact[n],p-2,p);
    for(int i=n-1;i>=0;--i){
        ifact[i]=(ifact[i+1]*(i+1))%p;
    }
}

ll fff(int n,int k,int p){
    if(k>n) return 0;
    if(k==0||k==n) return 1;
    ll res=(fact[n]*ifact[k])%p;
    res=(res*ifact[n-k])%p;
    return res;
}

int main(){
    cin>>n>>p>>k>>r;
    sb(n*k,p);
    ll sum=0;
    for(int i=0;i<=n;++i){
        int term=i*k+r;
        sum=(sum+fff(n*k,term,p))%p;
    }
    
    cout<<sum;
    return 0;
}
2024/7/11 10:12
加载中...