您的位置:首页 > 其它

【数论定理】卢卡斯定理

2016-08-05 16:57 204 查看

库卡斯模板:

/* Lucas定理:我们令n=sp+q , m=tp+r .(q ,r ≤p)
* 那么:C(n,m)=C(s,t)*C(q,r)
* 使用前要打出0~mod-1的阶乘表fac[]
* 时间O(logp(n)*p)
*/
const int mod=1000000007;
typedef long long ll;
ll fac[100018];
ll pow_m(ll a,ll n)
{
ll res=1;
while(n)
{
if(n%2) res*=a,res%=mod;
a=a*a;
a%=mod;
n/=2;
}
return res;
}
ll inv(ll a,ll mod)
{
return pow_m(a,mod-2);
}
ll C(ll n,ll m)
{
if(n<m) return 0;
return (fac
*inv(fac[m],mod)%mod)*inv(fac[n-m],mod)%mod;
}
ll lucas(ll n,ll m,ll mod)
{
ll res=0;
if(n+m==0) return 1;
return C(n%mod,m%mod)*lucas(n/mod,m/mod,mod)%mod;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: