#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
#define ll long long
const int inf=0x3f3f3f3f;
const int maxn=100000+10;
ll mod;
ll n,m;
ll f[maxn];
ll quick(ll a,ll b)
{
ll ans=1;
while(b)
{
if(b&1)
ans=((ans%mod)*(a%mod))%mod;
a=((a%mod)*(a%mod))%mod;
b>>=1;
}
return ans%mod;
}
ll niyuan(ll a,ll b)
{
return quick(a,b-2);
}
ll C(ll a,ll b)
{
if(b>a) return 0;
return f[a]*niyuan(f[b],mod)%mod*niyuan(f[a-b],mod)%mod;
}
void init()
{
ll sum=1;
f[0]=1;
for(int i=1; i<maxn; i++)
{
sum=(sum*i)%mod;
f[i]=sum;
}
//printf("fsaklf\n");
}
ll Lucas(ll n,ll m)
{
if(!m)
return 1;
return
C(n%mod,m%mod)*Lucas(n/mod,m/mod)%mod;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%lld%lld%lld",&n,&m,&mod);
init();
printf("%lld\n",Lucas(n+m,m));
}
return 0;
}
P3807 【模板】卢卡斯定理
最新推荐文章于 2023-05-24 21:31:51 发布