Now they turn to you for help, you should give them the answer. The result may be extremely huge; you should output the result modulo p, because squirrels can’t recognize large numbers.
Then followed T lines, each line contains three integers n, m, p, means that squirrels will save no more than m same beans in n different trees, 1 <= n, m <= 1000000000, 1 < p < 100000 and p is guaranteed to be a prime.
2 1 2 5 2 1 5
3 3
//
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
//计算C(a,b)%p p是素数且p<=100000
//求(a/b)%p,gcd(B,p)=1
__int64 exgcd(__int64 a,__int64 b,__int64& x,__int64& y)
{
if(b==0) return x=1,y=0,a;
__int64 r=exgcd(b,a%b,x,y);
__int64 t=x;
x=y;
y=t-(a/b)*y;
return r;
}
__int64 adivideb_mod_p(__int64 a,__int64 b,__int64 p)
{
__int64 x,y;
__int64 r=exgcd(b,p,x,y);
x*=a;
x=(x%p+p)%p;
return x;
}
//a<p,b<p p素数
__int64 fac[120000];
void init_mod_p(__int64 p)
{
fac[0]=1;
for(int i=1;i<p;i++) fac[i]=(fac[i-1]*i)%p;
}
__int64 comb_p(__int64 a,__int64 b,__int64 p)
{
if(b>a) return 0;
if(a==b) return 1;
__int64 tn,tm;
tn=fac[a];tm=(fac[a-b]*fac[b])%p;
return adivideb_mod_p(tn,tm,p);
}
//Lucas定理 answer
__int64 comb_mod_p(__int64 a,__int64 b,__int64 p)
{
if(b==0) return 1;
return (comb_p(a%p,b%p,p)*comb_mod_p(a/p,b/p,p))%p;
}
int main()
{
int ci;scanf("%d",&ci);
while(ci--)
{
__int64 n,m,p;
scanf("%I64d%I64d%I64d",&n,&m,&p);
init_mod_p(p);
printf("%I64d\n",comb_mod_p(n+m,n,p));
}
return 0;
}
//当p不固定且case很多的时候 可以改成下面这样
//计算C(a,b)%p p是素数且p<=100000
//求(a/b)%p,gcd(B,p)=1
bignum exgcd(bignum a,bignum b,bignum& x,bignum& y)
{
if(b==0) return x=1,y=0,a;
bignum r=exgcd(b,a%b,x,y);
bignum t=x;
x=y;
y=t-(a/b)*y;
return r;
}
bignum adivideb_mod_p(bignum a,bignum b,bignum p)
{
bignum x,y;
bignum r=exgcd(b,p,x,y);
x*=a;
x=(x%p+p)%p;
return x;
}
//a<p,b<p p素数
bignum comb_p(bignum a,bignum b,bignum p)
{
if(b>a) return 0;
if(a==b) return 1;
if(b>a-b) b=a-b;
bignum tn=1,tm=1;
//tn=fac[a];tm=(fac[a-b]*fac[b])%p;
for(int i=1;i<=b;i++)
{
tn=(tn*(a-i+1))%p;
tm=(tm*i)%p;
}
return adivideb_mod_p(tn,tm,p);
}
//Lucas定理 answer
bignum comb_mod_p(bignum a,bignum b,bignum p)
{
if(b==0) return 1;
return (comb_p(a%p,b%p,p)*comb_mod_p(a/p,b/p,p))%p;
}
//若b<=10000 b<p且p是素数 则用下面函数
//计算C(a,b)%p p是素数且b<=10000,b<p
bignum comb_mod_p(bignum a,bignum b,bignum p)
{
if(b>a) return 0;
if(a==b) return 1;
bignum tn=1,tm=1;
for(int i=1;i<=b;i++)
{
tn=(tn*(a-i+1))%p;
tm=(tm*i)%p;
}
return adivideb_mod_p(tn,tm,p);
}