您的位置:首页 > 其它

Lucas定理模版、

2016-09-08 20:05 211 查看
资料:传送门

相关题目:传送门

求C(n,m)模p的结果

#include<cmath>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long ll;
ll n,m,p;
ll quick_mod(ll a, ll b){
ll ans = 1;
a=a%p;
while(b){
if(b&1) ans = ans*a%p;
b>>=1;
a=a*a%p;
}
return ans;
}
ll C(ll n, ll m){
if(m>n) return 0;
if(n==m) return 1;
if(m>n-m) m = n-m;
ll ans=1, cns=1;
for(int i=1; i<=m; ++i){
ans=(ans*(n+i-m))%p;
cns=(cns*i)%p;
}
ll res = (ans*quick_mod(cns, p-2))%p;
return res;
}
ll Lucas(ll n, ll m){
if(m==0) return 1;
return C(n%p, m%p)%p * Lucas(n/p, m/p)%p;
}
int main(){
scanf("%lld%lld%lld",&n,&m,&p);
printf("%lld\n", Lucas(n, m));
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: