Lucas定理求组合数模板

证明过程:http://www.cnblogs.com/jianglangcaijin/p/3446839.html

$Lucas(n,m,p)=C(n\%p,m\%p)*Lucas(n/p,m/p,p)$

$C^n_m=\frac{n!}{m!(n-m)!}$

$x^{p-1}\equiv 1\ (mod p)\Longrightarrow x*x^{p-2}\equiv 1\ (mod p)\Longrightarrow x^{p-2}\equiv (1/x)^p\ (mod p)$

$C^n_m\equiv n!*[m!(n-m)!]^{p-2}\ (mod p)$

 1 #include <iostream>
 2 #include <cstdio>
 3 #define ll long long
 4 using namespace std;
 5 ll n,m,p,fac[100001];
 6 ll power(ll a,ll b,ll p)
 7 {
 8     ll t=a%p,s=1;
 9     while(b)
10     {
11         if (b%2==1) s=s*t%p;
12         t=t*t%p;
13         b/=2;
14     }
15     return s;
16 }
17 ll C(ll n,ll m,ll p)
18 {
19     if (m>n) return 0;
20     return fac[n]*power(fac[m]*fac[n-m],p-2,p)%p;
21 }
22 ll Lucas(ll n,ll m,ll p)
23 {
24     if (m==0) return 1;
25     return C(n%p,m%p,p)*Lucas(n/p,m/p,p)%p;
26 }
27 int main()
28 {
29     cin>>n>>m>>p;
30     fac[0]=1;
31     for (ll i=1;i<=p;i++) fac[i]=fac[i-1]*i%p;
32     cout<<Lucas(n,m,p)<<endl;
33 }

 

转载于:https://www.cnblogs.com/algonote/p/7610703.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值