【卡特兰数+大数】HDU1131Count the Trees

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


Problem Description
Another common social inability is known as ACM (Abnormally Compulsive Meditation). This psychological disorder is somewhat common among programmers. It can be described as the temporary (although frequent) loss of the faculty of speech when the whole power of the brain is applied to something extremely interesting or challenging. 
Juan is a very gifted programmer, and has a severe case of ACM (he even participated in an ACM world championship a few months ago). Lately, his loved ones are worried about him, because he has found a new exciting problem to exercise his intellectual powers, and he has been speechless for several weeks now. The problem is the determination of the number of different labeled binary trees that can be built using exactly n different elements. 

For example, given one element A, just one binary tree can be formed (using A as the root of the tree). With two elements, A and B, four different binary trees can be created, as shown in the figure. 

If you are able to provide a solution for this problem, Juan will be able to talk again, and his friends and family will be forever grateful. 

 

Input
The input will consist of several input cases, one per line. Each input case will be specified by the number n ( 1 ≤ n ≤ 100 ) of different elements that must be used to form the trees. A number 0 will mark the end of input and is not to be processed. 
 

Output
For each input case print the number of binary trees that can be built using the n elements, followed by a newline character. 
 

Sample Input
       
       
       
       
1 2 10 25 0
 

Sample Output
       
       
       
       
1 4 60949324800 75414671852339208296275849248768000000

题目意思:给你n个节点,然后求出能够组成的二叉树的个数,注意每个节点都有标号。

代码:

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
using namespace std;
const int N=10010;
string s[101];
string h[101];
string f[101];
string add(string aa,string bb)
{
    string ans="";
    int aLen=aa.size();
    int bLen=bb.size();
    int a[N]={0},b[N]={0};
    for(int i=0;i<aLen;i++)
        a[aLen-i-1]=aa[i]-'0';
    for(int i=0;i<bLen;i++)
        b[bLen-i-1]=bb[i]-'0';
    int MaxLen=max(aLen,bLen);
    for(int i=0;i<MaxLen;i++){
        a[i]+=b[i];
        if(a[i]>9){
            a[i+1]++;
            a[i]%=10;
        }
    }
    if(a[MaxLen]) MaxLen++;
    for(int i=MaxLen-1;i>=0;i--)
        ans+=a[i]+'0';
    return ans;
}
string mul_string(string aa,string bb)
{
    string ans="";
    int aLen=aa.size();
    int bLen=bb.size();
    int a[N]={0},b[N]={0},c[N]={0};
    for(int i=0;i<aLen;i++)
        a[aLen-i-1]=aa[i]-'0';
    for(int i=0;i<bLen;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;
}
string mul_int(string aa,int b)
{
    string ans="";
    int aLen=aa.size();
    int a[N]={0};
    for(int i=0;i<aLen;i++)
        a[aLen-i-1]=aa[i]-'0';
    int w=0;
    for(int i=0;i<aLen;i++){
        a[i]=a[i]*b+w;
        w=a[i]/10;
        a[i]%=10;
    }
    int MaxLen=aLen;
    while(w){
        a[MaxLen++]=w%10;
        w/=10;
    }
    for(int i=MaxLen-1;i>=0;i--)
        ans+=a[i]+'0';
    return ans;
}
void Catalan()
{
    h[0]="1";h[1]="1";
    for(int i=2;i<101;i++){
        for(int j=0;j<i;j++){
            h[i]=add(h[i],mul_string(h[j],h[i-j-1]));
        }
    }
}
void Fact()
{
    f[1]="1",f[0]="1";
    for(int i=2;i<101;i++)
        f[i]=mul_int(f[i-1],i);
}
string S(int n)
{
    return mul_string(f[n],h[n]);
}
int main()
{
    int n;
    Catalan();
    Fact();
    while(cin>>n&&n){
        if(s[n]!=""){
            cout<<s[n]<<endl;
            continue;
        }
        s[n]=S(n);
        cout<<s[n]<<endl;
    }
    return 0;
}


你可能感兴趣的:(【卡特兰数+大数】HDU1131Count the Trees)