思路:求c(m+n,n)%p
lucas:
a=ak*p^k+(ak-1)*p^(k-1)+(ak-2)*p^(k-2)+…+a0
b=bk*p^k+(bk-1)*p^(k-1)+(bk-2)*p^(k-2)+…+b0
C(a,b)=C(ak,bk)*C((ak-1),(bk-1))*…C(a0,b0)(modp)
#include<iostream> #include<cstdio> #include<cstring> using namespace std; long long m,n,p; long long f[100005]; void init() { f[0]=1; f[1]=1; for(int i=2;i<=p;i++)f[i]=(f[i-1]*i)%p; } long long quickpow(long long m,long long n,int k) { long long b = 1; while (n > 0) { if (n & 1) b = (b*m)%k; n = n >> 1 ; m = (m*m)%k; } return b; } long long C(long long a,long long b) { if(a<b)return 0; else return f[a]*quickpow(f[a-b]*f[b]%p,p-2,p)%p; } long long lucas(long long a,long long b) { if(b==0)return 1; else return (lucas(a/p,b/p)*C(a%p,b%p))%p; } int main() { int t; scanf("%d",&t); while(t--) { scanf("%lld%lld%lld",&n,&m,&p); init(); int ans=lucas(m+n,n)%p; cout<<ans<<endl; } return 0; }