Lucas 定理
其中 i = n/p , j = m/p , a = n % p , b = m % p
证明
令n = ()p
首先有
所以
对于左边的第m位的系数 , 即x^m 的系数 是C(n,m)
而对于右边 , x^m 的系数 是 , 即k = m/p , l = m%p
证毕
模板题
#include<bits/stdc++.h>
#define N 200050
#define LL long long
using namespace std;
LL T,n,m,p; LL fac[N],inv[N];
LL power(LL a,LL b,LL mod){
LL ans=1; for(;b;b>>=1){ if(b&1) ans=(ans*a) % p; a = (a*a) % p;} return ans;
}
LL C(LL a,LL b,LL mod){
if(b>a) return 0;
return ((fac[a] * power(fac[a-b], mod-2, mod)) % mod * power(fac[b], mod-2, mod)) % mod;
}
LL Lucas(LL x,LL y,LL mod){
if(y==0) return 1;
if(x<mod && y<mod) return C(x,y,mod);
else return (Lucas(x/mod,y/mod,mod) * C(x%mod,y%mod,mod)) % mod;
}
void Solve(){
scanf("%d%d%d",&n,&m,&p);
fac[0] = fac[1] = 1;
for(int i=2;i<=n+m;i++) fac[i] = (fac[i-1] * i) % p;
printf("%lld\n",Lucas(n+m,m,p));
}
int main(){
scanf("%d",&T); while(T--) Solve(); return 0;
}