1023 Train Problem II(卡特兰数)

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
 

 

Sample Output
1 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

 

最初所写的代码(没考虑范围):

 1 #include <stdio.h>
 2 #include <algorithm>
 3 int Catalan(int n){
 4     if(n<=1)
 5         return 1;
 6     int *h = new int [n+100000];
 7     h[0] = h[1] = 1;
 8     for(int i=2;i<=n;i++){
 9         h[i]=0;
10         for(int j=0;j<i;j++){
11             h[i]+=(h[j]*h[i-1-j]);
12         }
13     }
14     int result=h[n];
15     delete []h;
16     return result;
17 }
18 int main()
19 {
20     int n;
21     while(~scanf("%d",&n)){
22         printf("%d\n",Catalan(n));
23     }
24     return 0;
25 }

 

 

bin神的模板:

 1 #include <stdio.h>
 2 
 3 //*******************************
 4 //打表卡特兰数
 5 //第 n个 卡特兰数存在a[n]中,a[n][0]表示长度;
 6 //注意数是倒着存的,个位是 a[n][1] 输出时注意倒过来。 
 7 //*********************************
 8 int a[105][100];
 9 void ktl()
10 {
11     int i,j,yu,len;
12     a[2][0]=1;
13     a[2][1]=2;
14     a[1][0]=1;
15     a[1][1]=1;
16     len=1;
17     for(i=3;i<101;i++)
18     {
19         yu=0;
20         for(j=1;j<=len;j++)
21         {
22             int t=(a[i-1][j])*(4*i-2)+yu;
23             yu=t/10;
24             a[i][j]=t%10;
25         }    
26         while(yu)
27         {
28             a[i][++len]=yu%10;
29             yu/=10;
30         }
31         for(j=len;j>=1;j--)
32         {
33             int t=a[i][j]+yu*10;
34             a[i][j]=t/(i+1);
35             yu = t%(i+1);
36         }        
37         while(!a[i][len])
38         {
39             len--;
40         }    
41         a[i][0]=len;
42     }    
43     
44 }    
45 int main()
46 {
47     ktl();
48     int n;
49     while(scanf("%d",&n)!=EOF)
50     {
51         for(int i=a[n][0];i>0;i--)
52         {
53             printf("%d",a[n][i]);
54         }    
55         puts("");
56     }    
57     return 0;
58 }

 

posted @ 2015-08-05 16:49  Vmetrio  阅读(157)  评论(0编辑  收藏  举报