【卡特兰数+高精度】HDU1023Train Problem II

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

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.

代码:

#include<iostream>
#include<string>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
string s[110];
//  高精度加法;
string add(string aa,string bb)
{
    string ans="";
    int a[10100]={0},b[10100]={0};
    int aLen=aa.size();
    int bLen=bb.size();
    int MaxLen=max(aLen,bLen);
    for(int i=aLen-1;i>=0;i--)
        a[aLen-i-1]=aa[i]-'0';
    for(int i=bLen-1;i>=0;i--)
        b[bLen-i-1]=bb[i]-'0';
    for(int i=0;i<MaxLen;i++){
        a[i]+=b[i];
        if(a[i]>=10){
            a[i+1]++;
            a[i]%=10;
        }
    }
    if(a[MaxLen]) ans=a[MaxLen]+'0';
    for(int i=MaxLen-1;i>=0;i--)
        ans+=a[i]+'0';
    return ans;
}
//  高精度乘法;
string mul(string aa,string bb)
{
    string ans="";
    int a[10100]={0},b[10100]={0},c[10100]={0};
    int aLen=aa.size();
    int bLen=bb.size();
    for(int i=aLen-1;i>=0;i--)
        a[aLen-i-1]=aa[i]-'0';
    for(int i=bLen-1;i>=0;i--)
        b[bLen-i-1]=bb[i]-'0';
    for(int i=0;i<aLen;i++)
        for(int j=0;j<bLen;j++)
            c[i+j]+=a[i]*b[j];
    for(int i=0;i<=aLen+bLen;i++){
        c[i+1]+=c[i]/10;
        c[i]%=10;
    }
    int MaxLen=aLen+bLen;
    while(c[MaxLen]==0) MaxLen--;
    for(int i=MaxLen;i>=0;i--)
        ans+=c[i]+'0';
    return ans;
}
int main()
{
    s[0]="1";s[1]="1";
    for(int i=2;i<101;i++){
        s[i]="0";
        for(int j=0;j<i;j++){
            s[i]=add(s[i],mul(s[j],s[i-j-1]));
        }
    }
    int n;
    while(cin>>n){
        cout<<s[n]<<endl;
    }
    return 0;
}


你可能感兴趣的:(【卡特兰数+高精度】HDU1023Train Problem II)