先给一份洛谷模板题的代码
#include<bits/stdc++.h> using namespace std; #define lowbit(x) x&-x #define ll long long #define pii pair<ll,ll> #define dob double #define For(i,s,n) for(ll i = s;i <= n;i++) #define mem0(a) memset(a,0,sizeof a) #define gcd(a,b) __gcd(a,b) #define lcm(a,b) a/gcd(a,b)*b const int N = 2e5+50; const double eps = 1e-6; const ll mod = 998244353; ll p; ll fac[N]; ll f_pow(ll a,ll b){ ll res = 1; while(b){ if(b&1) res = res%p*a%p%p; a = a%p*a%p%p; b>>=1; } return res; } void init(){ fac[0] = 1; for(ll i = 1;i <= p;i++)fac[i] = fac[i-1]*i%p; } ll C(ll n,ll m){ if(m > n) return 0; return fac[n]*f_pow(fac[m],p-2)%p*f_pow(fac[n-m],p-2)%p; } ll Lucas(ll n,ll m){ if(m == 0) return 1; return Lucas(n/p,m/p)*C(n%p,m%p)%p; } int main(){ //ios::sync_with_stdio(false); ll t;cin>>t; while(t--){ ll n,m; cin>>n>>m>>p; init(); cout<<Lucas(n+m,n)<<endl; } return 0; }