您的位置:首页 > 其它

[模板] 卢卡斯定理

2018-03-29 09:00 274 查看

题目描述:

求Cmn+m% pCn+mm% p

保证p为质数

题目分析:

卢卡斯定理
Cmn% p=Cm%pn%p∗Cm/pn/p%pCnm% p=Cn%pm%p∗Cn/pm/p%p

题目链接:

Luogu 3807

Ac 代码:

// luogu-judger-enable-o2
#include <cstdio>
#include <iostream>
#include <cstring>
#define ll long long
ll num[210000];
ll mod;
ll fastpow(ll x,ll y)
{
ll ans=1;
x%=mod;
for(;y;y>>=1,x=(x*x)%mod)
if(y&1) ans=(ans*x)%mod;
return ans;
}
ll C(ll n,ll m)
{
if(n<m) return 0ll;
ll up=1ll,down=1ll;
for(ll i=n-m+1;i<=n;i++) (up*=i)%=mod;
for(ll i=2;i<=m;i++) (down*=i)%=mod;
return up*fastpow(down,mod-2)%mod;
}
ll Lucas(ll n,ll m)
{
ll ans=1;
while(n&&m)
{
ans=(ans*C(n%mod,m%mod))%mod;
n/=mod,m/=mod;
}
return ans;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
ll n,m;
scanf("%lld%lld%lld",&n,&m,&mod);
printf("%lld\n",Lucas(n+m,m));
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: