题目描述:
求Cmn+m% pCn+mm% p
保证p为质数
题目分析:
卢卡斯定理
Cmn% p=Cm%pn%p∗Cm/pn/p%pCnm% p=Cn%pm%p∗Cn/pm/p%p
题目链接:
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;
}