Lucas求条@LLi_1111

· · 闲话

#include <bits/stdc++.h>
using namespace std;
#define ll long long
ll n,m,p;
bool vis[10000009];
ll P[100009];
int cnt;
void Linearsieve()
{
    for(int i=2;i<1000000;i++)
    {
        if(!vis[i]) P[++cnt]=i;
        for(int j=1;j<=cnt&&i*P[j]<1000000;j++)
        {
            vis[i*P[j]]=1;
            if(i%P[j]==0) break;
        }
    }
    return;
}
ll qpow(ll a,ll b,ll p)
{
    ll res=1;
    a%=p;
    while(b)
    {
        if(b&1) res=res*a%p;
        a=a*a%p;
        b>>=1;
    }
    return res;
}
ll Legendre(ll n,ll p)
{
    ll sum=0;
    while(n)
    {
        sum+=n/p;
        n/=p;
    }
    return sum;
}
ll C(ll n,ll m,ll p)
{
    ll ans=1;
    for(int i=1;i<=cnt;i++)
    {
        ll e=Legendre(n,P[i])-Legendre(m,P[i])-Legendre(n-m,P[i]);
        if(e>0) ans=(ans*qpow(P[i],e,p)%p)%p;
    }
    return ans%p;
}
ll lucus(ll n,ll m,ll p)
{
    if(m==0) return 1;
    return C(n%p,m%p,p)*lucus(n/p,m/p,p)%p;
}
int main()
{
    int T;
    scanf("%d",&T);
    Linearsieve();
    while(T--)
    {
        scanf("%lld%lld%lld",&n,&m,&p);
        m=n+m;
        printf("%lld\n",lucus(m,n,p));
    }



    return 0;
}