您的位置:首页 > 其它

2016夏季练习——数论

2016-07-14 11:13 295 查看
来源: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
*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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: