您的位置:首页 > 编程语言 > Java开发

hdu1131(卡特兰数)

2013-12-15 16:49 411 查看

Count the Trees

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

Total Submission(s): 1309    Accepted Submission(s): 860


[align=left]Problem Description[/align]
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.

 

[align=left]Input[/align]
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.

 

[align=left]Output[/align]
For each input case print the number of binary trees that can be built using the n elements, followed by a newline character.

 

[align=left]Sample Input[/align]

1
2
10
25
0

 

[align=left]Sample Output[/align]

1
4
60949324800
75414671852339208296275849248768000000

 

[align=left]Source[/align]
UVA
 

[align=left]Recommend[/align]
Eddy   |   We have carefully selected several similar problems for you:  1130 1134 1133 1267 1100 
      本体给定彼此不同的节点数目,要求能构造多少不同的二叉树。
     由于二叉树的定义具有递归性。分支思想,枚举左右节点的节点数,然后就是每棵树都可以全排,即最终结果*!N.本题n个节点,可以构造左右子树节点数为(i,n-1-i)(其中i为0到n-1)。
f
=∑f[i]*f[n-1-i].这是个很经典的数学模型。
     数据很容易增大,建议Java解决。
import java.math.BigInteger;
import java.util.Scanner;

public class hdu1131 {

static BigInteger []f=new BigInteger[110];

static void catlan()
{
f[0]=new BigInteger("1");
f[1]=new BigInteger("1");
for(int i=2;i<101;i++)
{
f[i]=new BigInteger("0");
for(int j=0;j<i;j++)
{
f[i]=f[i].add( f[j].multiply(f[i-j-1]) );
}
}
}

static void GetSequense()
{
for(int i=1;i<101;i++)
{
BigInteger tmp=new BigInteger("1");
BigInteger One=new BigInteger("1");
for(int j=1;j<=i;j++)
{

f[i]=f[i].multiply(tmp);
tmp=tmp.add(One);
}
}
}

public static void main(String[] str)
{
Scanner input=new Scanner(System.in);
catlan();
GetSequense();
int i;
while(input.hasNext())
{
i=input.nextInt();
if(0==i)break;
System.out.println(f[i]);
}
}
}


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