Count the TreesTime
limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 1798 Accepted Submission (s): 1195
Problem Descriptionanother 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 in speech when the whole power of the brain I s applied to something extremely interesting or challenging.
Juan is a very gifted programmer, and have a severe case of ACM (he even participated in an ACM World championship a few MO Nths ago). Lately, his loved ones is worried about him, because he had found a new exciting problem to exercise his intellectual POW ERS, and he have been speechless for several weeks now. The problem is the determination of the number of different labeled binary trees so can be built using exactly n differe NT elements.
For example, given one element A, just one binary tree can is formed (using A as the root of the tree). With-elements, A and B, four different binary trees can is created, as shown in the figure.
If you is able to provide a solution for this problem, Juan would be able to talk again, and his friends and family would b e forever grateful.
Inputthe input would consist of several input cases, one per line. Each input case is specified by the number n (1≤n≤100) of different elements that must being used to form the tree S. A number 0 would mark the end of input and is not to be processed.
Outputfor each input case print the number of binary trees that can is built using the n elements, followed by a newline C Haracter.
Sample Input
1210250
Sample Output
146094932480075414671852339208296275849248768000000
Sourceuva
Recommendeddy | 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)); {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! Number of *catalan
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Hdoj-1131-count the Trees