Train Problem II
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 0 Accepted Submission(s) : 0
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 16796HintThe result will be very large, so you may not process it by 32-bit integers.
Author
Ignatius.L
Statistic |
Submit |
Back
#include <iostream>
using namespace std;
int a[101][101]= {0}; //大数卡特兰数
int main()
{
int i,j,n,k,b[101],len;
a[1][0] = 1; //n辆车,对应第一维,a存的值为方案数
b[1] = len = 1; //卡特兰数的长度
for(i = 2;i < 101;i++)
{
//大數乘法 處理
for(j = 0;j < len;j++)
a[i][j] = (4*i-2) * a[i-1][j]; //组合公式,一维为控制数组的递推
for(k = j = 0;j < len;j++)
{
a[i][j] += k;
k = a[i][j] / 10; //k对应进阶值
a[i][j] %= 10;
}
while (k) //最后一位,控制进阶
{
a[i][len++] = k%10;
k /= 10;
}
//大數除法處理
for(j = len-1,k = 0;j >= 0;j--) //处理相乘结果
{
a[i][j] += k*10;
k = a[i][j] % (i+1); //递推公式
a[i][j] /= (i+1);
}
while(!a[i][len-1]) //高位零处理
len--;
b[i] = len;
}
while(cin >> n)
{
for(i = b[n] - 1;i >= 0;i--)
cout << a[n][i];
cout << endl;
}
return 0;
}