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
1
2
3
10
2
3
10
Sample Output
1
2
5
16796
2
5
16796
Hint
The result will be very large, so you may not process it by 32-bit integers.
Author
Ignatius.L
卡特兰数相关了解:
http://blog.csdn.net/wuzhekai1985/article/details/6764858
http://www.cnblogs.com/kuangbin/archive/2012/03/21/2410519.html
http://baike.baidu.com/link?url=SIrdv9w3YrmZdmGR5dIIszcQfzjndTjD9fHo1qzCYatFgsZIiwSbb2zOY70ouVcYZYES_1sbIXYuD1hbLaN78K
最初所写的代码(没考虑范围):
#include <stdio.h>
#include <algorithm>
int Catalan(int n){
if(n<=)
return ;
int *h = new int [n+];
h[] = h[] = ;
for(int i=;i<=n;i++){
h[i]=;
for(int j=;j<i;j++){
h[i]+=(h[j]*h[i--j]);
}
}
int result=h[n];
delete []h;
return result;
}
int main()
{
int n;
while(~scanf("%d",&n)){
printf("%d\n",Catalan(n));
}
return ;
}
bin神的模板:
#include <stdio.h> //*******************************
//打表卡特兰数
//第 n个 卡特兰数存在a[n]中,a[n][0]表示长度;
//注意数是倒着存的,个位是 a[n][1] 输出时注意倒过来。
//*********************************
int a[][];
void ktl()
{
int i,j,yu,len;
a[][]=;
a[][]=;
a[][]=;
a[][]=;
len=;
for(i=;i<;i++)
{
yu=;
for(j=;j<=len;j++)
{
int t=(a[i-][j])*(*i-)+yu;
yu=t/;
a[i][j]=t%;
}
while(yu)
{
a[i][++len]=yu%;
yu/=;
}
for(j=len;j>=;j--)
{
int t=a[i][j]+yu*;
a[i][j]=t/(i+);
yu = t%(i+);
}
while(!a[i][len])
{
len--;
}
a[i][]=len;
} }
int main()
{
ktl();
int n;
while(scanf("%d",&n)!=EOF)
{
for(int i=a[n][];i>;i--)
{
printf("%d",a[n][i]);
}
puts("");
}
return ;
}