首页 > 其他 > 详细

hdoj-1131-Count the Trees

时间:2015-08-11 18:42:20      阅读:174      评论:0      收藏:0      [点我收藏+]

Count the Trees

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1798 Accepted Submission(s): 1195


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

Source

Recommend
Eddy | We have carefully selected several similar problems for you: 1130 1133 1267 2067 1141

#include<stdio.h> 
#include<string.h>
const int maxn=500;
int catalan[101][maxn];
void make(){
	catalan[1][0]=1;
	int i,j,t,res;
	int temp[maxn];
	memset(temp,0,sizeof(temp));
	for(i=2;i<=100;++i){
		t=4*i-2;
		for(j=0;j<maxn;++j){
			catalan[i][j]+=catalan[i-1][j]*t;
			
		}
		for(j=0;j<maxn;++j){
			if(catalan[i][j]>=10){
				catalan[i][j+1]+=catalan[i][j]/10;
				catalan[i][j]%=10;
			}
		}
		t=i+1;res=0;
		for(j=maxn-1;j>=0;--j){
			res=res*10+catalan[i][j];
			temp[j]=res/t;
			res%=t;
		}
		for(j=maxn-1;j>=0;--j){
			catalan[i][j]=temp[j];
		}
	}
	
}
void f(int n){
	int i,j;
	for(i=1;i<=n;++i){
		for(j=0;j<maxn;++j){
			catalan[n][j]*=i;
		}
		for(j=0;j<maxn;++j){
			if(catalan[n][j]>=10){
				catalan[n][j+1]+=catalan[n][j]/10;
				catalan[n][j]%=10;
			}
		}
	}
}
int main(){
	make();
	int n;
	while(~scanf("%d",&n),n){
		f(n);
	    int i=maxn-1;
	    while(catalan[n][i]==0) --i;
	    for(;i>=0;--i){
	    	printf("%d",catalan[n][i]);
	    }
	    printf("\n");
	}
	return 0;
}

n!*catalan数

版权声明:本文为博主原创文章,未经博主允许不得转载。

hdoj-1131-Count the Trees

原文:http://blog.csdn.net/qq_18062811/article/details/47424219

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!