UVa 10007 - Count the Trees(卡特兰数+阶乘+大数)

题目链接:UVa 10007

题意:统计n个节点的二叉树的个数

1个节点形成的二叉树的形状个数为:1

2个节点形成的二叉树的形状个数为:2

3个节点形成的二叉树的形状个数为:5

4个节点形成的二叉树的形状个数为:14

5个节点形成的二叉树的形状个数为:42

把n个节点对号入座有n!种情况

所以有n个节点的形成的二叉树的总数是:卡特兰数F[n]*n!

程序:

 1 import java.math.BigInteger;
 2 import java.util.Scanner;
 3 public class Main {
 4     public static void main(String args[]){
 5         Scanner cin=new Scanner(System.in);
 6         BigInteger h[]=new BigInteger[302];
 7         BigInteger a[]=new BigInteger[302];
 8         BigInteger fact=BigInteger.valueOf(1);
 9         int n;
10         h[1]=BigInteger.valueOf(1);
11         a[1]=BigInteger.valueOf(1);
12         for(int i=2;i<=300;i++){
13             fact=fact.multiply(BigInteger.valueOf(i));
14             BigInteger k=BigInteger.valueOf(i*4-2);
15             h[i]=h[i-1].multiply(k);
16             h[i]=h[i].divide(BigInteger.valueOf(i+1));
17             a[i]=h[i].multiply(fact);
18             //System.out.println(" "+h[i]+" "+fact+" "+a[i]);
19         }
20         while(true){
21             n=cin.nextInt();
22             if(n==0)
23                 break;
24             System.out.println(a[n]);
25         }
26     }
27 }

 

posted @ 2015-08-09 17:17  一样菜  阅读(399)  评论(0编辑  收藏  举报