数论 卡特兰数模板

Saving Beans

题目链接

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<math.h>
using namespace std;
typedef long long LL;
LL p;
LL f[100004];
void init()
{
    int i;
    f[0]=1;
    for(i=1;i<=p;i++)
        f[i]=(f[i-1]*i)%p;
}
LL power(LL x,LL y,LL mod)
{
    LL res=1;
    while(y)
    {
        if(y&1)
            res=(res*x)%mod;
        x=(x*x)%mod;
        y>>=1;
    }
    return res;
}
LL C(LL a,LL b)
{
    if(b>a)
        return 0;
    return f[a]*power((f[a-b]*f[b])%p,p-2,p)%p;
}
LL Lucas(LL a,LL b)
{
    if(b==0) return 1;
    return (C(a%p,b%p)*Lucas(a/p,b/p))%p;
}
int main()
{

    int T;
    scanf("%d",&T);
    while(T--)
    {
        LL n,m;
        scanf("%lld %lld %lld",&n,&m,&p);
        init();
        printf("%lld\n",Lucas(m+n,m)%p);
    }
}

猜你喜欢

转载自blog.csdn.net/qq_41006240/article/details/84944812