您的位置:首页 > 其它

HDU 1131 Count the Trees 卡特兰数的应用

2014-06-29 22:17 330 查看

Count the Trees

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 1504 Accepted Submission(s): 1001


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个节点,能构成多少种不同的二叉树?
  (能构成h(N)个)
但因为每一个节点都被命名了,也就是每一个节点都当做是不同的,
所以最后每一个卡特兰数都要乘以n!
F(n)=F(n-1)*(4n-2)/(n+1)

递推的方式变了 
F(n)=F(n-1)*(4n-2)*n!/(n+1)
F(n)=F(n-1)(n-1!)*(4n-2)*n/(n+1)
F(n)=F(n-1)*(4n-2)*n/(n+1)
这题要把a数组后面的位数开大点 
*/
#include<iostream>
using namespace std;
int a[101][1001]={0};//a[i]表示的是第i个数 
                    //因为是大数 [j]表示的是这个数的若干位 
int main(){
    int b[101],i,j,n,k,z; 

        a[1][0]=1;
        b[1]=1;
        k=1;//长度 
        for(i=2;i<101;i++)
        {
            for(j=0;j<k;j++)
                a[i][j]=a[i-1][j]*(4*i-2)*i;//乘法大数 每位乘以
            
            z=0;//进位 
            for(j=0;j<k;j++)
            {
                a[i][j]+=z;
                z=a[i][j]/10;
                a[i][j]%=10;
            } 
            while(z)//仍有进位 
            {
                a[i][k++]=z%10;
                z/=10;
            }
            
            //大数除法 模拟除法 从高到低
            z=0; 
            for(j=k-1;j>=0;j--)
            {
                a[i][j]+=z*10;//上一位剩的
                z=a[i][j]%(i+1);
                a[i][j]/=(i+1);
            } 
            while(!a[i][k-1])//去除前面的0 
                k--;
            b[i]=k; //保存n的大数的长度 
        }
        
        while(cin>>n&&n)
        {
        
            for(i=b
-1;i>=0;i--)
                cout<<a
[i];
            cout<<endl;
        }     
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: