HDU 1023 Train Problem II(卡特兰数)

来源:互联网 发布:克而瑞数据2017年排名 编辑:程序博客网 时间:2024/06/16 18:04

与卡特兰数有关的博客:http://blog.163.com/lz_666888/blog/static/1147857262009914112922803/

http://blog.csdn.net/niushuai666/article/details/6936859

Train Problem II

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7994    Accepted Submission(s): 4286


Problem Description
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
12310
 

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

Author
Ignatius.L
 

Recommend

We have carefully selected several similar problems for you:  1133 1022 1130 1131 1134 


Catalan数的组合公式为 Cn=C(2n,n) / (n+1);

递推公式为 h(n ) = h(n-1)*(4*n-2) / (n+1)


代码:

#include<stdio.h>#include<string.h>using namespace std;int a[101][101];int b[101];int main(){    a[1][0]=1;    b[1]=1;    int len=1;    int r,j;    for(int i=2;i<=100;i++)    {        int t=i-1,temp;        for(j=0;j<len;j++)        {            a[i][j]=a[i-1][j]*(4*t+2);        }        for(j=0,r=0;j<len;j++)        {            temp=a[i][j]+r;            a[i][j]=temp%10;            r=temp/10;        }        while(r)        {            a[i][len++]=r%10;            r=r/10;        }        for(int j=len-1,r=0;j>=0;j--)        {            temp=r*10+a[i][j];            a[i][j]=temp/(t+2);            r=temp%(t+2);        }        while(!a[i][len-1])            len--;        b[i]=len;    }int n;    while(~scanf("%d",&n))    {        for(int j=b[n]-1;j>=0;j--)        {            printf("%d",a[n][j]);        }        printf("\n");    }}

1.括号化问题。

  矩阵链乘: P=a1×a2×a3×……×an,依据乘法结合律,不改变其顺序,只用括号表示成对的乘积,试问有几种括号化的方案?(h(n)种)

2.出栈次序问题。


  一个栈(无穷大)的进栈序列为1,2,3,..n,有多少个不同的出栈序列?


  类似:有2n个人排成一行进入剧场。入场费5元。其中只有n个人有一张5元钞票,另外n人只有10元钞票,剧院无其它钞票,问有多少中方法使得只要有10元的人买票,售票处就有5元的钞票找零?(将持5元者到达视作将5元入栈,持10元者到达视作使栈中某5元出栈)


3.将多边行划分为三角形问题。


  将一个凸多边形区域分成三角形区域的方法数?


  类似:一位大城市的律师在她住所以北n个街区和以东n个街区处工作。每天她走2n个街区去上班。如果她从不穿越(但可以碰到)从家到办公室的对角线,那么有多少条可能的道路?


      类似:在圆上选择2n个点,将这些点成对连接起来使得所得到的n条线段不相交的方法数?


4.给顶节点组成二叉树的问题。


  给定N个节点,能构成多少种不同的二叉树?

Catalan数的解法


1.Catalan数的组合公式为 Cn=C(2n,n) / (n+1);
2.此数的递归公式为 h(n ) = h(n-1)*(4*n-2) / (n+1)。
令h(1)=1,h(0)=1,catalan数满足递归式:
  h(n)= h(0)*h(n-1)+h(1)*h(n-2) + ... + h(n-1)h(0) (其中n>=2)
  例如:h(2)=h(0)*h(1)+h(1)*h(0)=1*1+1*1=2
  h(3)=h(0)*h(2)+h(1)*h(1)+h(2)*h(1)=1*2+1*1+2*1=5
  另类递归式:
  h(n)=h(n-1)*(4*n-2)/(n+1);
  该递推关系的解为:
  h(n)=C(2n,n)/(n+1) (n=1,2,3,...)


0 0
原创粉丝点击