Lucas
Lucas //求c(n,m)%p 其中p是质数
# include <bits/stdc++.h>
using namespace std;
typedef long long LL;
LL mulit(LL a,LL b,LL mod)
{
LL res=0;
while(b){
if(b&1) res=(res+a)%mod;
a=(a<<1)%mod;
b>>=1;
}
return res;
}
LL quick_pow(LL a,LL b,LL mod)
{
LL ret=1;
while(b){
if(b&1) ret=mulit(ret,a,mod);
a=mulit(a,a,mod);
b>>=1;
}
return ret;
}
LL comp(LL a,LL b,LL mod)
{
if(a<b) return 0;
if(a==b) return 1;
if(b>(a-b)) b=a-b;
LL ans=1,ca=1,cb=1;
for(int i=0;i<b;i++) ca=ca*(a-i)%mod,cb=cb*(b-i)%mod;
ans=ca*quick_pow(cb,mod-2,mod)%mod;//ca*inv(cb) 此处是用了费马小定理求逆元,当然也可以用扩展gcd
return ans;
}
LL lucas(LL a,LL b,LL mod)
{
if(a<mod&&b<mod) return comp(a,b,mod);
return comp(a%mod,b%mod,mod)*lucas(a/mod,b/mod,mod)%mod;
}