卢卡斯定理

 

Lucas 定理


by---cxlove

Lucas 定理:A、B是非负整数,p是质数。AB写成p进制:A=a[n]a[n-1]...a[0],B=b[n]b[n-1]...b[0]。

则组合数C(A,B)与C(a[n],b[n])*C(a[n-1],b[n-1])*...*C(a[0],b[0])  modp同

即:Lucas(n,m,p)=c(n%p,m%p)*Lucas(n/p,m/p,p) 

For non-negative integers m and n and a prime p, the following congruence relation holds:


where


and


are the base p expansions of m and n respectively.

 

首先我们注意到 n=(ak...a2,a1,a0)p  =  (ak...a2,a1)p * p + a0

                                                       =  [n/p]*p+a0


                                                    且m=[m/p]+b0

 

只要我们更够证明 C(n,m)=C([n/p],[m/p]) * C(a0,b0)  (mod p)

剩下的工作由归纳法即可完成

 

我们知道对任意质数p:   (1+x)^p  == 1+(x^p)  (mod p) 

注意!这里一定要是质数  ................(为什么)

 

对 模p 而言

 上式左右两边的x^m的系数对模p而言一定同余(为什么),其中左边的x^m的系数是 C(n,m) 而由于a0和b0都小于p

右边的x^m ( = x^(([m/p]*p)+b0)) 一定是由 x^([m/p]*p) 和 x^b0 相乘而得 (即发生于 i=[m/p] , j=b0 时) 因此我们就有了

 

C(n,m)=C([n/p],[m/p]) * C(a0,b0)  (mod p) 


HDU 3037

http://acm.hdu.edu.cn/showproblem.php?pid=3037

基本的组合数学,C(N+M,M)然后对P取模

[cpp]  view plain  copy
  1. #include<iostream>  
  2. #include<cstdio>  
  3. #include<ctime>  
  4. #include<cstring>  
  5. #include<cstdlib>  
  6. #include<vector>  
  7. #define C    240  
  8. #define TIME 10  
  9. #define LL long long  
  10. using namespace std;  
  11. LL PowMod(LL a,LL b,LL MOD){  
  12.     LL ret=1;  
  13.     while(b){  
  14.         if(b&1) ret=(ret*a)%MOD;  
  15.         a=(a*a)%MOD;  
  16.         b>>=1;  
  17.     }  
  18.     return ret;  
  19. }  
  20. LL fac[100005];  
  21. LL Get_Fact(LL p){  
  22.     fac[0]=1;  
  23.     for(int i=1;i<=p;i++)  
  24.         fac[i]=(fac[i-1]*i)%p;  
  25. }  
  26. LL Lucas(LL n,LL m,LL p){  
  27.     LL ret=1;  
  28.     while(n&&m){  
  29.         LL a=n%p,b=m%p;  
  30.         if(a<b) return 0;  
  31.         ret=(ret*fac[a]*PowMod(fac[b]*fac[a-b]%p,p-2,p))%p;  
  32.         n/=p;  
  33.         m/=p;  
  34.     }  
  35.     return ret;  
  36. }  
  37. int main(){  
  38.     int t;  
  39.     scanf("%d",&t);  
  40.     while(t--){  
  41.         LL n,m,p;  
  42.         scanf("%I64d%I64d%I64d",&n,&m,&p);  
  43.         Get_Fact(p);  
  44.         printf("%I64d\n",Lucas(n+m,m,p));  
  45.     }  
  46.     return 0;  
  47. }  

HDU 4399 Xiao Ming's Hope

http://acm.hdu.edu.cn/showproblem.php?pid=4349

Lucas定理推广

[cpp]  view plain  copy
  1. #include<iostream>  
  2. #include<cstdio>  
  3. #include<ctime>  
  4. #include<cstring>  
  5. #include<cstdlib>  
  6. #include<vector>  
  7. #define C    240  
  8. #define TIME 10  
  9. #define LL long long  
  10. using namespace std;  
  11. int main(){  
  12.     int n;  
  13.     while(scanf("%d",&n)!=EOF){  
  14.         int cnt=0;  
  15.         while(n){  
  16.             if(n&1)  
  17.                cnt++;  
  18.             n>>=1;  
  19.         }  
  20.         printf("%d\n",1<<cnt);  
  21.     }  
  22.     return 0;  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值