HDU 1023 Train Problem II ——————Catalan 数

版权声明:听说这里是写版权声明的 https://blog.csdn.net/Hpuer_Random/article/details/81871349

Train Problem II

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


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.

 


Catalan 数打表

#include<bits/stdc++.h>
using namespace std;
const int MAXN=110;
int a[MAXN][MAXN];//a[i][0] save the length

void GetCatalans()
{
    int carry=0;
    int len=1;
    a[1][1]=1; a[1][0]=1;
    a[2][1]=2; a[2][0]=1;
    for(int i=3;i<MAXN;i++)
    {

        for(int j=1;j<=len;j++)
        {
            int sum = a[i-1][j]*(i*4-2) + carry;
              carry = sum / 10;
            a[i][j] = sum%10;
        }
        while(carry)
        {
            a[i][++len] = carry%10;
            carry /= 10;
        }


        for(int j=len;j>0;j--)
        {
            int sum = a[i][j] + carry*10;
            a[i][j] = sum / (i+1);
              carry = sum % (i+1);
        }
        while(a[i][len]==0) len--;
        a[i][0]=len;
    }
}
int main()
{
    int n;
    GetCatalans();
    while(~scanf("%d",&n))
    {
        for(int i=a[n][0];i>0;i--)
            printf("%d",a[n][i]);
        printf("\n");
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Hpuer_Random/article/details/81871349