题目:给出n,m,p,求C(n+m,m)然后对p取模
p<=1e5
代码:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<algorithm>
#include<ctime>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<list>
#include<numeric>
using namespace std;
#define LL long long
#define ULL unsigned long long
#define INF 0x3f3f3f3f3f3f3f3f
#define mm(a,b) memset(a,b,sizeof(a))
#define PP puts("*********************");
template<class T> T f_abs(T a){ return a > 0 ? a : -a; }
template<class T> T gcd(T a, T b){ return b ? gcd(b, a%b) : a; }
template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}
// 0x3f3f3f3f3f3f3f3f
const int maxn=1e5+50;
LL fac[maxn];
void get_fac(LL p){
fac[0]=1;
for(int i=1;i<=p;i++)
fac[i]=fac[i-1]*i%p;
}
LL pow_mod(LL a,LL b,LL p){
LL res=1;
while(b>0){
if(b&1)
res=res*a%p;
a=a*a%p;
b/=2;
}
return res;
}
LL lucas(LL n,LL m,LL p){
LL res=1;
while(n&&m){
LL a=n%p,b=m%p;
if(a<b) return 0;
res=(res*fac[a]%p)*pow_mod(fac[b]*fac[a-b]%p,p-2,p)%p;
n/=p;
m/=p;
}
return res;
}
int main(){
int T;
LL n,m,p;
scanf("%d",&T);
while(T--){
scanf("%lld%lld%lld",&n,&m,&p);
get_fac(p);
printf("%lld\n",lucas(n+m,m,p));
}
return 0;
}