Code(luogu P3807):
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #define ll long long 5 #define MN 100005 6 using namespace std; 7 inline int in(){ 8 int x=0;bool f=0; char c; 9 for (;(c=getchar())<'0'||c>'9';f=c=='-'); 10 for (x=c-'0';(c=getchar())>='0'&&c<='9';x=(x<<3)+(x<<1)+c-'0'); 11 return f?-x:x; 12 } 13 ll fct[MN]; 14 int n,m,p,T; 15 inline int qpow(ll x,int k){ 16 ll res=1ll;while (k){ 17 if (k&1) res=(res*x)%p; 18 x=(x*x)%p;k>>=1; 19 }return res; 20 } 21 inline ll calc(ll x,ll y){ 22 return (x<y)?0ll:(fct[x]*qpow(fct[y],p-2)%p*qpow(fct[x-y],p-2))%p; 23 } 24 inline ll lucas(ll x,ll y){ 25 return (y)?(calc(x%p,y%p)*lucas(x/p,y/p))%p:1ll; 26 } 27 int main() 28 { 29 T=in();while (T--){ 30 n=in();m=in();p=in();fct[0]=1ll; 31 for (int i=1;i<=n+m;++i) fct[i]=(1ll*fct[i-1]*i)%p; 32 printf("%d ",lucas(n+m,n)%p); 33 }return 0; 34 }