HDU 1023 Train Problem II(卡特兰数)

来源:互联网 发布:钣金外壳 软件 编辑:程序博客网 时间:2024/06/10 15:51
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
12310
 

Sample Output
12516796
Hint
The result will be very large, so you may not process it by 32-bit integers.
#include <iostream>using namespace std;int A[105][105],b[105];//A[i][j]存的是数i的卡特兰,b[i]存的是卡特兰数i的长度void catalan() //求卡特兰数{    int i, j, len, carry, temp;    A[1][0] = b[1] = 1;    len = 1;    for(i = 2; i <= 100; i++)    {        for(j = 0; j < len; j++) //乘法        A[i][j] = A[i-1][j]*(4*(i-1)+2);        carry = 0;        for(j = 0; j < len; j++) //处理相乘结果        {            temp = A[i][j] + carry;            A[i][j] = temp % 10;            carry = temp / 10;        }        while(carry) //进位处理        {            A[i][len++] = carry % 10;            carry /= 10;        }        carry = 0;        for(j = len-1; j >= 0; j--) //除法        {            temp = carry*10 + A[i][j];            A[i][j] = temp/(i+1);            carry = temp%(i+1);        }        while(!A[i][len-1]) //高位零处理        len --;        b[i] = len;    }}int main(){    int N;    catalan(); //求卡特兰数    while(cin>>N)    {        for(int i=b[N]-1;i>=0;i--)            cout<<A[N][i];        cout<<endl;    }    return 0;}
0 0
原创粉丝点击