您的位置:首页 > 其它

hdu-1131 Count the trees

2014-08-25 12:06 246 查看




Count the Trees

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

Total Submission(s): 1572    Accepted Submission(s): 1044


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

 
   
   本题为经典的卡特兰数问题。在不考虑顺序(即字母顺序),将结点编号为0~n-1;任取一个节点k作根节点,从而衍生出两个子问题f(k-1)和f(n-k),有f(k-1)*f(n-k)棵树;则

   f(n)=f(0)(n-1)+f(1)f(n-1)+.......+f(n-1)*f(0);符合卡特兰数的递推公式。详见点击打开链接。有该递推公式可以推出f(n)=f(n-1)*(4n-2)/(n+1);好!现在卡特兰数的问题解决了,来看

   加入字母顺序以后,这可以看成已经准备好了n个位置,现在要按排座位,这是个排序问题,排序总数为n!种!所以数的数量就等于:f(n)*n!;别!别以为现在就解决问题了。来看看题目吧!n可以达到100。f(100)的位数可以达到60~80!具体自己去数吧!而100!的位数可以达到160左右。任何数值类型都不能容纳!所以又是个高精度问题。本想

先算出1-100的卡特兰数,在算出1-100的阶乘,再相乘!但很不理想!两个都是大数。很复杂!还没涉及!所以改成h(n)=h(n-1)*n*(4n-2)/(n+1)(h(n)为数的数量).这里乘法和除法要分开进行。

 来看看我的代码吧!

#include <stdio.h>
#include <iostream>
#include <string.h>
#include <queue>
#include <algorithm>
#include <math.h>
#define MIN(a,b) (a) < (b) ? (a) : (b)
#define M 250 //结果的最长位数
using namespace std;
int num[100][M]; //存放1~100的树的数量
int temp[M];
void init()
{
int i,j,c,temp;
num[1][0]=1;

for(i=2;i<=100;i++)//枚举1-100
{

for(j=0,c=0;j<M;j++) //乘于i*(4*i-2);
{
num[i][j]=num[i-1][j]*i*(4*i-2)+c;
c=num[i][j]/10;
num[i][j]%=10;
}
for(j=M-1,c=0;j>=0;j--) //除于(i+1);
{
temp=num[i][j]+c*10;
num[i][j]=temp/(i+1);
c=temp%(i+1);

}
}

}
//输出结果
void print(int n)
{
int i,flag=0;//flag用于标记发现数字的最高位,如果没有发现,则数为0
for(i=M-1;i>=0;i--)
{
if(num
[i]||flag)  //发现以后无论0还是非零都输出
{
printf("%d",num
[i]);
flag=1;
}
}
if(!flag)
printf("0");
printf("\n");
}
int main()
{
int n,T;

init();
while(scanf("%d",&n)&&n>0)
{
memset(temp,0,sizeof(temp));//对temp初始化为0
print(n);
}
return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息