比赛 组合计数1 评测结果 AAAAAAEEEEAAAAAAEEEE
题目名称 组合数问题 最终得分 60
用户昵称 ChenBp 运行时间 1.596 s
代码语言 C++ 内存使用 7.65 MiB
提交时间 2026-02-26 09:21:34
显示代码纯文本
#include <iostream>
using namespace std;
const int N=1e6+6;
typedef long long ll;
ll n,p,k,r;
ll ksm(ll x,ll y,ll mod){
    ll res=1;
    while(y){
        if(y&1){
            res=res*x%mod;
        }
        x=x*x%mod;
        y>>=1;
    }
    return res;
}
ll jc[N];
ll c(ll x,ll y,ll mod){
    return jc[x]*ksm(jc[y]*jc[x-y]%mod,mod-2,mod)%mod;
}
int main(){
    freopen("problem.in","r",stdin);
    freopen("problem.out","w",stdout);
    cin>>n>>p>>k>>r;
    jc[0]=1;
    for(int i=1;i<=n*k;i++){
        jc[i]=jc[i-1]*i%p;
    }
    ll ans=0;
    for(int i=0;i*k+r<=n*k;i++){
        ans=(ans+c(n*k,i*k+r,p))%p;
    }
    cout<<ans;
    return 0;
}