您的位置:首页 > 大数据 > 人工智能

hdoj.1023 Train Problem II【卡特兰数列】 2015/08/27

2015-08-27 10:08 375 查看

Train Problem II

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 6927 Accepted Submission(s): 3749

[align=left]Problem Description[/align]
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.

[align=left]Input[/align]
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.

[align=left]Output[/align]
For each test case, you should output how many ways that all the trains can get out of the railway.

[align=left]Sample Input[/align]

1
2
3
10


[align=left]Sample Output[/align]

1
2
5
16796

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


[align=left]Author[/align]
Ignatius.L

注:卡特兰数列模板题
#include<iostream>
#include<cstdio>
#include<cstring>

using namespace std;

int c[101][100],b[100];
void catalan(){
memset(c,0,sizeof(c));
c[1][0]=1;b[1]=1;
int i,j,k,len=1,flag;
for( i=2;i<=100;++i ){
k = 4 * i - 2;
flag = 0;
for( j = 0 ; j < len ; ++j ) //乘
c[i][j] = c[i-1][j] * k;
for( j = 0 ; j < len ; ++j ){ // 进位
k = c[i][j] + flag;
c[i][j] = k % 10;
flag = k / 10;
}
while( flag ){ //进位
c[i][len++] = flag%10;
flag /= 10;
}
flag = 0;
for( j=len-1;j>=0;--j ){ // 除
k = flag * 10 + c[i][j];
c[i][j] = k / (i+1);
flag = k % (i+1);
}
while( !c[i][len-1] ) //高位除零
len--;
b[i] = len; // 记录当前项的位数
}
}

int main(){
int n,i,ans=1;
catalan();
while( ~scanf("%d",&n) ){
for( i = b
-1 ; i >= 0 ; --i )
printf("%d",c
[i]);
printf("\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: