预处理阶乘。纯板子,做一下保存。
#include <bits/stdc++.h>
using namespace std;
const int maxn=1e5+10;
typedef long long ll;
ll frac[maxn];
void get(int p){
frac[0]=1;
for(int i=1;i<=p+10;i++)frac[i]=frac[i-1]*i%p;
}
ll qpow(ll a,ll b,ll p){
ll res=1;
while(b){
if(b&1)res=res*a%p;
a=a*a%p;
b>>=1;
}
return res;
}
ll c(ll n,ll m,ll p){
if(m>n)return 0;
return frac[n]*qpow(frac[m],p-2,p)%p*qpow(frac[n-m],p-2,p)%p;
}
ll lucas(ll n,ll m,ll p){
return m==0?1:lucas(n/p,m/p,p)*c(n%p,m%p,p)%p;
}
int main(){
int t;cin>>t;
while(t--){
ll n,m,p;
cin>>n>>m>>p;
get(p);
cout<<lucas(n+m,m,p)<<"
";
}
return 0;
}