卡特兰数专题训练

1.进栈出栈序列组合数问题
HDU - 1023 Train Problem II
As we all know the Train Problem I, the boss of the Ignatius Train Station want to know if all the trains come in strict-increasing order, how many orders that all the trains can get out of the railway.
Input
The input contains several test cases. Each test cases consists of a number N(1<=N<=100). The input is terminated by the end of file.
Output
For each test case, you should output how many ways that all the trains can get out of the railway.
Sample Input
1
2
3
10
Sample Output
1
2
5
16796

Hint
The result will be very large, so you may not process it by 32-bit integers.

#include<bits/stdc++.h>
using namespace std;
int a[105][100];
void ktl()
{
   int i,j,yu,len;
   a[1][0]=1;
   a[1][1]=1;
   len=1;
   for(i=2;i<101;i++)
   {
       yu=0;
       for(j=1;j<=len;j++)
       {
           int t=(a[i-1][j])*(4*i-2)+yu;
           yu=t/10;
           a[i][j]=t%10;
       }
       while(yu)
       {
           a[i][++len]=yu%10;
           yu/=10;
       }
       for(j=len;j>=1;j--)
       {
           int t=a[i][j]+yu*10;
           a[i][j]=t/(i+1);
           yu=t%(i+1);
       }
       while(!a[i][len])
       {
           len--;
       }
       a[i][0]=len;
   }
}
int main()
{
   ktl();
   int n;
   while(scanf("%d",&n)!=EOF)
   {
       for(int i=a[n][0];i>0;i--)
       {
           printf("%d",a[n][i]);
       }
       puts("");
   }
   return 0;
}

2.n * n棋盘从左上角走到右下角,不穿越对角线问题,下三角方法数*2。

递推式:h(n)= h(0)*h(n-1)+h(1)*h(n-2) + … + h(n-1)*h(0) (n>=2)

小兔的棋盘 HDU - 2067
小兔的叔叔从外面旅游回来给她带来了一个礼物,小兔高兴地跑回自己的房间,拆开一看是一个棋盘,小兔有所失望。不过没过几天发现了棋盘的好玩之处。从起点(0,0)走到终点(n,n)的最短路径数是C(2n,n),现在小兔又想如果不穿越对角线(但可接触对角线上的格点),这样的路径数有多少?小兔想了很长时间都没想出来,现在想请你帮助小兔解决这个问题,对于你来说应该不难吧!
Input
每次输入一个数n(1<=n<=35),当n等于-1时结束输入。
Output
对于每个输入数据输出路径数,具体格式看Sample。
Sample Input
1
3
12
-1
Sample Output
1 1 2
2 3 10
3 12 416024

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
LL h[36];
int kase,n;
void init()
{
    int i,j;

    h[0]=h[1]=1;
    for(i=2;i<36;i++)
    {
  	h[i]=0;
	for(j=0;j<i;j++)
            h[i]=h[i]+h[j]*h[i-j-1];
    }
}

int main()
{
    init();
    while(scanf("%d",&n)!=EOF)
    {
        if(n==-1)
        break;
        printf("%d %d %lld\n",++kase,n,h[n]*2);
    }
    return 0;
}

3.排列顺序敏感的n个节点构造二叉树种类数:

R(x)=h(x)*n!

int t=(a[i-1][j])*(4*i-2)*i+yu;
//多乘以个 i 即可
//h( n ) = ( ( 4*n-2 )/( n+1 )*h( n-1 ) );


//*******************************
//打表卡特兰数
//第 n个 卡特兰数存在a[n]中,a[n][0]表示长度;
//注意数是倒着存的,个位是 a[n][1] 输出时注意倒过来。
//*********************************
#include<bits/stdc++.h>
using namespace std;
int a[105][250];
void ktl()
{
    int i,j,yu,len;
    a[1][0]=1;
    a[1][1]=1;
    len=1;
    for(i=2;i<101;i++)
    {
        yu=0;
        for(j=1;j<=len;j++)
        {
            //int t=(a[i-1][j])*(4*i-2)+yu;
            //如果是求考虑顺序的排列,如不同点敏感的n节点的二叉树种类则改成这句即可
            int t=(a[i-1][j])*(4*i-2)*i+yu;
            yu=t/10;
            a[i][j]=t%10;
        }
        while(yu)
        {
            a[i][++len]=yu%10;
            yu/=10;
        }
        for(j=len;j>=1;j--)
        {
            int t=a[i][j]+yu*10;
            a[i][j]=t/(i+1);
            yu=t%(i+1);
        }
        while(!a[i][len])
        {
            len--;
        }
        a[i][0]=len;
    }
}
int main()
{
    ktl();
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        if(n==0)
            break;
        for(int i=a[n][0];i>0;i--)
        {
            printf("%d",a[n][i]);
        }
        puts("");
    }
    return 0;
}

4.n节点构造二叉搜索树种类数同二叉树,这里n个节点不敏感,不考虑顺序

//h( n ) = ( ( 4*n-2 )/( n+1 )*h( n-1 ) );


//*******************************
//打表卡特兰数
//第 n个 卡特兰数存在a[n]中,a[n][0]表示长度;
//注意数是倒着存的,个位是 a[n][1] 输出时注意倒过来。 
//*********************************
#include<bits/stdc++.h>
using namespace std;
int a[105][250];
void ktl()
{
    int i,j,yu,len;
    a[1][0]=1;
    a[1][1]=1;
    len=1;
    for(i=2;i<101;i++)
    {
        yu=0;
        for(j=1;j<=len;j++)
        {
            int t=(a[i-1][j])*(4*i-2)+yu;
            //如果是求考虑顺序的排列,如不同点敏感的n节点的二叉树种类则改成这句即可
            //int t=(a[i-1][j])*(4*i-2)*i+yu;
            yu=t/10;
            a[i][j]=t%10;
        }
        while(yu)
        {
            a[i][++len]=yu%10;
            yu/=10;
        }
        for(j=len;j>=1;j--)
        {
            int t=a[i][j]+yu*10;
            a[i][j]=t/(i+1);
            yu=t%(i+1);
        }
        while(!a[i][len])
        {
            len--;
        }
        a[i][0]=len;
    }
}
int main()
{
    ktl();
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        for(int i=a[n][0];i>0;i--)
        {
            printf("%d",a[n][i]);
        }
        puts("");
    }
    return 0;
}

java1求卡特兰大数模板:

import java.io.*;  
import java.math.BigInteger;  
import java.util.*; 
 
public class Main {
	
    public static void main(String[] args) {
    	Scanner cin=new Scanner(System.in);
    	BigInteger s[]=new BigInteger[105];
    	s[1]=BigInteger.ONE;
    	for(int i=2;i<105;i++){
    		s[i]=s[i-1].multiply(BigInteger.valueOf((4*i-2))).divide(BigInteger.valueOf(i+1));
    	}
    	while(cin.hasNext()){
    		int n=cin.nextInt();
    		
    		System.out.println(s[n]);
    	}
     }
}

5.一个圆被2n个点分割,两两相连形成n对不相交线段,连接方式数为h(n)
POJ - 2084 Game of Connections
This is a small but ancient game. You are supposed to write down the numbers 1, 2, 3, . . . , 2n - 1, 2n consecutively in clockwise order on the ground to form a circle, and then, to draw some straight line segments to connect them into number pairs. Every number must be connected to exactly one another.
And, no two segments are allowed to intersect.
It’s still a simple game, isn’t it? But after you’ve written down the 2n numbers, can you tell me in how many different ways can you connect the numbers into pairs? Life is harder, right?
Input
Each line of the input file will be a single positive number n, except the last line, which is a number -1.
You may assume that 1 <= n <= 100.
Output
For each n, print in a single line the number of ways to connect the 2n numbers into pairs.
Sample Input
2
3
-1
Sample Output
2
5

//h( n ) = ( ( 4*n-2 )/( n+1 )*h( n-1 ) );


//*******************************
//打表卡特兰数
//第 n个 卡特兰数存在a[n]中,a[n][0]表示长度;
//注意数是倒着存的,个位是 a[n][1] 输出时注意倒过来。
//*********************************
#include<iostream>
#include<cstdio>
using namespace std;
int a[105][250];
void ktl()
{
    int i,j,yu,len;
    a[1][0]=1;
    a[1][1]=1;
    len=1;
    for(i=2;i<101;i++)
    {
        yu=0;
        for(j=1;j<=len;j++)
        {
            int t=(a[i-1][j])*(4*i-2)+yu;
            //如果是求考虑顺序的排列,如不同点敏感的n节点的二叉树种类则改成这句即可
            //int t=(a[i-1][j])*(4*i-2)*i+yu;
            yu=t/10;
            a[i][j]=t%10;
        }
        while(yu)
        {
            a[i][++len]=yu%10;
            yu/=10;
        }
        for(j=len;j>=1;j--)
        {
            int t=a[i][j]+yu*10;
            a[i][j]=t/(i+1);
            yu=t%(i+1);
        }
        while(!a[i][len])
        {
            len--;
        }
        a[i][0]=len;
    }
}
int main()
{
    ktl();
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        if(n==-1)
            break;
        for(int i=a[n][0];i>0;i--)
        {
            printf("%d",a[n][i]);
        }
        puts("");
    }
    return 0;
}

6.求卡特兰数前n项和并取模m
取模需要用到逆元
详解:https://blog.csdn.net/w1304636468/article/details/90182213

HDU - 3240 Counting Binary Trees
There are 5 distinct binary trees of 3 nodes:

在这里插入图片描述
Let T(n) be the number of distinct non-empty binary trees of no more than n nodes, your task is to calculate T(n) mod m.
Input
The input contains at most 10 test cases. Each case contains two integers n and m (1 <= n <= 100,000, 1 <= m <= 10 9) on a single line. The input ends with n = m = 0.
Output
For each test case, print T(n) mod m.
Sample Input
3 100
4 10
0 0
Sample Output
8
2

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e5+10;
ll Cat[maxn];
int num[maxn];
int fat[maxn],tot;
void find_fat(int val){
    tot=0;
    for(int i=2;i*i<=val;i++)
    {
        if(val%i==0)
        {
            fat[++tot]=i;
        }
        while(val%i==0)
            val/=i;
    }
    if(val>1) fat[++tot]=val;
}
int exgcd(int a,int b,int &x,int &y)
{
    int d=a;
    if(b!=0)
    {
        d=exgcd(b,a%b,y,x);
        y-=a/b*x;
    }
    else
    {
        x=1;
        y=0;
    }
    return d;
}
int inverse(int a,int m)
{
    int x,y;
    exgcd(a,m,x,y);
    return (x%m+m)%m;
}

int calc(int val,int ok)
{
    for(int i=1;i<=tot;i++)
    {
        while(val%fat[i]==0)
        {
            if(ok) num[i]++;
            else num[i]--;
            val/=fat[i];
        }
    }
    return val;
}
int main()
{
    int n,m;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        if(n==0&&m==0)
            break;
        memset(num,0,sizeof(num));
        find_fat(m);
        Cat[1]=1;
        ll ans=1;
        for(int i=2;i<=n;i++)
        {
            Cat[i]=Cat[i-1]*calc(4*i-2,1)%m;
            Cat[i]=Cat[i]*inverse(calc(i+1,0),m)%m;
            ll tmp=Cat[i];
            for(int k=1;k<=tot;k++)
                {
                    for(int j=1;j<=num[k];j++)
                    {
                        tmp=tmp*fat[k]%m;
                    }
                }
                ans=(ans+tmp)%m;
        }
        printf("%lld\n",ans);
    }
    return 0;
}

7.卡特兰数对1e9+7取模(n和模一定互质,直接求逆元即可)
一般给出模m=1e9+7,默认所有n都是与模m互质的。直接求逆元即可。因为n均小于1e9+7,所以n和1e9+7只有公因子1。
zcmu-1934
1934: ly的二叉树
Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 42 Solved: 9
[Submit][Status][Web Board]

Description
某一天,ly正在上数据结构课。老师在讲台上面讲着二叉树,ly在下面发着呆。
突然ly想到一个问题:对于一棵n个无编号节点的有根二叉树,有多少种形态呐?你能告诉她吗?

Input
多组输入,处理到文件结束
每一组输入一行,一个正整数n(1≤n≤1000000),意义如题目所述。

Output
每组数据输出一行,包含一个正整数表示答案,由于数字可能非常大,你只需要把最后的结果对1000000007取模即可。

Sample Input
3

Sample Output
5

卡特兰数公式:h(n)=h(n-1)(4n-2)/(n+1);这里直接取模是不对的。因为涉及了除法,所以我们改成成(n+1)的逆元。

 
#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define N 1000005
#define MOD 1000000007
long long ans[N];
void Egcd(LL a,LL b,LL &x,LL &y)//扩展欧几里德
{
    if(b==0)
    {
        x=1;
        y=0;
        return ;  
    }
    Egcd(b,a%b,x,y);  
    LL tmp=x;
    x=y;
    y=tmp-a/b*y;  
}
int main()
{
    int n;
    ans[0] = 0, ans[1] = 1;  
    for(int i=2; i<=N; i++)
    {
        LL x,y;
        Egcd(i+1,MOD,x,y);//求i+1的乘法逆元x
        ans[i]=ans[i-1]*(4*i-2)%MOD*(x%MOD+MOD)%MOD;  
    }
    while(~scanf("%d",&n))
    {
        printf("%lld\n",ans[n]);
    }
    return 0;
}

可以修改为快速求第n个卡特兰数取模mod从O(n*log(n))降到O(n)

#include <iostream>
#include <cmath>
#include <ctime>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <set>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <algorithm>
#define ULL unsigned long long
using namespace std;

long long n;
const long long M=1000000007;
long long inv[1000010];
long long last,now=1;

void init()
{
    inv[1]=1;
    for(int i=2;i<=n+1;i++)inv[i]=(M-M/i)*inv[M%i]%M;
}

int main()
{
    scanf("%lld",&n);
    init();
    for(int i=2;i<=n;i++)
    {
        last=now;
        now=last*(4*i-2)%M*inv[i+1]%M;
    }
    printf("%lld\n",last);
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值