来源:HDU3037
Lucas定理模板题
中间的取逆元是费马小定理
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long LL;
const int MAXN = 100000+10;
LL n,m,p;
LL MOD;
LL fac[MAXN];
void ini(){
MOD=p;
fac[0]=1;
for(int i=1;i<=p;i++)
fac[i]=fac[i-1]*i%MOD;
}
LL quick_pow(LL a,LL n){
LL res=1;
LL temp=a%MOD;
while(n){
if(n&1) res=(res*temp)%MOD;
temp=temp*temp%MOD;
n>>=1;
}
return res;
}
int combination(LL n,LL k){
if(k>n) return 0;
return fac[n]*quick_pow(fac[k]*fac[n-k],p-2)%MOD;//Fermat ans p is a prime
}
LL lucas(LL n,LL k){
if(k==0) return 1;
else
return (combination(n%p,k%p)*lucas(n/p,k/p))%p;
}
int main(){
int T;
scanf("%d",&T);
while(T--){
scanf("%d%d%d",&n,&m,&p);
ini();
cout<<lucas(m+n,n)<<endl;
}
return 0;
}
756

被折叠的 条评论
为什么被折叠?



