卢卡斯Lucas
证明戳这里
好吧背过就好反正就一行
#include<bits/stdc++.h>
#define N 100010
using namespace std;
typedef long long ll;
ll fac[N];
int T,p,n,m;
ll qpow(ll a,ll b){
ll ans=1;
while(b){
if(b&1)ans=(ans*a)%p;
b>>=1;
a=(a*a)%p;
}
return ans;
}
ll C(ll n,ll m){
if(m>n)return 0;
return ((fac[n]*qpow(fac[m],p-2))%p*qpow(fac[n-m],p-2))%p;
}
ll lucas(ll n,ll m){
if(!m)return 1;
return lucas(n/p,m/p)*C(n%p,m%p)%p;
}
int main(){
scanf("%d",&T);
while(T--){
scanf("%d%d%d",&n,&m,&p);
fac[0]=1;
for(int i=1;i<=p;i++) fac[i]=(fac[i-1]*i)%p;
printf("%lld
",lucas(n+m,n));
}
return 0;
}
扩展卢卡斯exLucas
https://www.cnblogs.com/Jackpei/p/10884719.html
https://blog.csdn.net/hqddm1253679098/article/details/82897638
https://www.cnblogs.com/zyt1253679098/p/9736741.html
gugugu掌握中国剩余定理后再说