HDU 1131 Count the Trees

8 篇文章 0 订阅
7 篇文章 0 订阅

Count the Trees
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2092 Accepted Submission(s): 1363

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


令c[n]=n个相同的点组成的树的形态数
例如题目中n=2的图这里写图片描述
认为树1和树3是一样的 树2和树4是一样的
所以上图一共有2种形态 c[2]=2
不难发现 c[n]*n!就等于每个点都不同时的树的数量(其实就是从点无差别到有差别 乘以一个全排列就可以了)
然后再来分析
当n=4时
有4种情况
①:根节点的左子树有3个点 没有右子树(有c[3]种方案)
②:根节点的右子树有3个点 没有左子数(有c[3]种方案)
③:左子树2个点 右子树1个点 (c[2]*c[1])
④:右子树1个点 左子树2个点(c[1]*c[2])
所以c[4]=c[3]*c[0]+c[2]*c[1]+c[1]*c[2]+c[0]*c[4]
这不就是卡特兰数的通项公式
h(n)= h(0)*h(n-1)+h(1)*h(n-2) + … + h(n-1)h(0) (n>=2)
so 按这公式 加上大数运算就可以了

#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<string>
#include<vector>
#include<deque>
#include<queue>
#include<algorithm>
#include<set>
#include<map>
#include<stack>
#include<time.h>
#include<math.h>
#include<list>
#include<cstring>
#include<fstream>
#include<bitset>
//#include<memory.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define pii pair<int,int>
#define INF 1000000007

struct BigNum{
    deque<int>num;
    //构造函数
    BigNum(){
        num.push_back(0);
    }
    BigNum(int x){
        if(!x)
            num.push_back(0);
        while(x){
            num.push_front(x%10);
            x/=10;
        }
    }
    //重载运算符
    BigNum operator+(const BigNum&x){//大数加法
        BigNum res;
        const deque<int>&a=num,&b=x.num;
        deque<int>&c=res.num;
        int alen=a.size(),blen=b.size();
        c.resize(max(alen,blen));
        for(int i=1,len=c.size();i<=c.size();++i){
            if(alen>=i)
                c[len-i]+=a[alen-i];
            if(blen>=i)
                c[len-i]+=b[blen-i];
        }
        for(int i=c.size()-1;i>0;--i){
            c[i-1]+=c[i]/10;
            c[i]%=10;
        }
        if(c[0]>9){
            c[0]-=10;
            c.push_front(1);
        }
        return res;
    }
    BigNum operator*(const BigNum&x){//大数乘法
        int zeros=0;
        BigNum res,t;
        const deque<int>&a=this->num,&b=x.num;
        deque<int>&tem=t.num;
        for(int i=b.size()-1;i>=0;--i){
            tem.clear();
            for(int j=0;j<a.size();++j)
                tem.push_back(a[j]*b[i]);
            for(int j=0;j<zeros;++j)
                tem.push_back(0);
            for(int j=tem.size()-1;j>0;--j){
                tem[j-1]+=tem[j]/10;
                tem[j]%=10;
            }
            if(tem[0]>9){
                tem.push_front(tem[0]/10);
                tem[1]%=10;
            }
            res=res+t;
            ++zeros;
        }
        while(res.num.size()>1&&res.num[0]==0)
            res.num.pop_front();
        return res;
    }
};

ostream&operator<<(ostream&out,BigNum&x){
    deque<int>&a=x.num;
    for(int i=0;i<a.size();++i)
        cout<<a[i];
    return out;
}

BigNum c[201];
BigNum jc[101];

void init(){
    c[0]=c[1]=BigNum(1);
    for(int i=2;i<=200;++i)
        for(int j=0;j<=i-2;j+=2)
            c[i]=c[i]+c[j]*c[i-2-j];
    jc[0]=jc[1]=BigNum(1);
    for(int i=2;i<=100;++i)
        jc[i]=jc[i-1]*BigNum(i);
}

int main()
{
    //freopen("/home/lu/文档/r.txt","r",stdin);
    //freopen("/home/lu/文档/w.txt","w",stdout);
    int n;
    init();
    BigNum res;
    while(cin>>n&&n){
        res=c[2*n]*jc[n];
        cout<<res<<endl;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值