传送门:acm.hdu.edu.cn/showproblem.php?pid=3037
Lucas定理入门题
Lucas定理:
Lucas(n,m,p)=C(n%p,m%p)*Lucas(n/p,m/p,p)%p
Lucas(n,0,p)=1
当然p是质数
参考文献;
http://blog.csdn.net/acm_cxlove/article/details/7844973
Code:
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long lld;
lld n,m,p;
lld fac[100001];
lld power(lld x,lld k,lld p){
lld ans=1;
for(;k;k>>=1){
if(k&1)ans=(ans*x)%p;
x=(x*x)%p;
}return ans%p;
}
lld inv(lld x,lld p){
return power(x,p-2,p)%p;
}
lld Lucas(lld n,lld m,lld p){
lld res=1;
while(n&&m){
lld a=n%p,b=m%p;
if(a<b) return 0;
res=(res*fac[a]*inv(fac[b]*fac[a-b]%p,p))%p;
n/=p; m/=p;
}
return res;
}
void getfac(lld p){
fac[0]=1;
for(lld i=1;i<=p;i++){
fac[i]=fac[i-1]*i%p;
}
}
int main(){
int T;
cin>>T;
while(T--){
cin>>n>>m>>p;
getfac(p);
cout<<Lucas(n+m,m,p)%p<<endl;
}
return 0;
}
本文详细介绍了Lucas定理的基本概念、公式及其在编程中的应用实例,通过实例代码帮助读者快速掌握Lucas定理的使用方法。

330

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



