[HDOJ1023]Train Problem II

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1023

 

Train Problem II

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


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.
 
 
( 卡特兰数可以表示一个栈有多少个不同的出栈序列)
大数卡特兰数的裸题,关于卡特兰数的介绍在这里:http://www.cppblog.com/MiYu/archive/2010/08/07/122573.html
 
代码:
 
 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <cmath>
 4 using namespace std;
 5 
 6 //Catalan
 7 int cat[105][105];    //大数卡特兰
 8 int clen[105];         //卡特兰数的长度
 9 int n;
10 
11 void catalan() {
12     int i, j, len, carry, temp;
13     cat[1][0] = clen[1] = 1;
14     len = 1;
15     for(i = 2; i <= 100; i++) {
16         for(j = 0; j < len; j++)
17             cat[i][j] = cat[i-1][j]*(4*(i-1)+2);
18         carry = 0;
19         for(j = 0; j < len; j++) {
20             temp = cat[i][j] + carry;
21             cat[i][j] = temp % 10;
22             carry = temp / 10;
23         }
24         while(carry) {
25             cat[i][len++] = carry % 10;
26             carry /= 10;
27         }
28         carry = 0;
29         for(j = len-1; j >= 0; j--) {
30             temp = carry*10 + cat[i][j];
31             cat[i][j] = temp/(i+1);
32             carry = temp%(i+1);
33         }
34         while(!cat[i][len-1]) {
35             len --;
36         }
37         clen[i] = len;
38     }
39 }
40 
41 void solve() {
42     for(int i = clen[n] - 1; i >= 0; i--) {
43         printf("%d", cat[n][i]);
44     }
45     printf("
");
46 }
47 
48 int main() {
49     catalan();
50     while(scanf("%d", &n) != EOF) {
51         solve();
52     }
53     return 0;
54 }
原文地址:https://www.cnblogs.com/kirai/p/4753147.html