AcWing 889 满足条件的01序列(卡特兰数)

本文介绍了一种计算从(0,0)到(n,n)路径数量的方法,要求路径上每个点的横坐标始终大于纵坐标。通过使用卡特兰数公式,结合模运算和快速幂算法,实现了一个高效的C++程序来解决这个问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在这里插入图片描述

思路:将本题转化为从(0,0)坐标起(n,n)为终点坐标的路径中要求每个点的横坐标要大于纵坐标,这样的路径有几条。其为卡特兰数

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <set>
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define space putchar(' ')
#define enter putchar('\n')
typedef pair<int ,int> PII;
const int mod=1e9+7;
const int N=1e5+10;


  ll gcd(ll a,ll b){ return b==0?a:gcd(b,a%b); }

ll lcm(ll a,ll b)
{
    return a*(b/gcd(a,b));
}

template <class T>
void read(T &x){
    char c;
    bool op = 0;
    while(c = getchar(), c < '0' || c > '9')
	if(c == '-') op = 1;
    x = c - '0';
    while(c = getchar(), c >= '0' && c <= '9')
	x = x * 10 + c - '0';
    if(op) x = -x;
}
template <class T>
void write(T x){
    if(x < 0) x = -x, putchar('-');
    if(x >= 10) write(x / 10);
    putchar('0' + x % 10);
}
ll ksm(ll a,ll k,ll p)
{
    ll res=1;
    while(k)
    {
        if(k&1)res=res*a%p;
        a=a*a%p;
        k>>=1;
    }
    return res;
}

int main()
{
   ll n;
   read(n);
   ll a=2*n,b=n;
   ll res=1;
   for(int i=a;i>a-b;i--)
   {
       res=res*i%mod;
   }
   for(int i=1;i<=b;i++)
   {
       res=res*ksm(i,mod-2,mod)%mod;
   }
   res=res*ksm(n+1,mod-2,mod)%mod;
   write(res);



    return 0;
}







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值