HDU3017:Lucas定理及详解

题目中要求的就是:

组合数学稍作推证可知:

剩余部分就是裸的Lucas定理了。

Luca定理的叙述如下:

  

递归写法:

(另附数论好题:http://www.cnblogs.com/linyujun/p/5199684.html)

代码实现:

 1 LL PowMod(LL a,LL b,LL MOD){//快速幂
 2     LL ret=1;  
 3     while(b){  
 4         if(b&1) ret=(ret*a)%MOD;  
 5         a=(a*a)%MOD;  
 6         b>>=1;  
 7     }  
 8     return ret;  
 9 }  
10 LL fac[100005];  
11 LL Get_Fact(LL p){//初始化
12     fac[0]=1;  
13     for(int i=1;i<=p;i++)  
14         fac[i]=(fac[i-1]*i)%p;  
15 }  
16 LL Lucas(LL n,LL m,LL p){//Lucas 定理
17     LL ret=1;  
18     while(n&&m){  
19         LL a=n%p,b=m%p;  
20         if(a<b) return 0;  
21         ret=(ret*fac[a]*PowMod(fac[b]*fac[a-b]%p,p-2,p))%p;  
22         n/=p;  
23         m/=p;  
24     }  
25     return ret;  
26 }
原文地址:https://www.cnblogs.com/poler/p/7681878.html