首页 > 其他 > 详细

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

时间:2015-08-09 18:29:16      阅读:258      评论:0      收藏:0      [点我收藏+]

题目链接: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 }

 

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

原文:http://www.cnblogs.com/mypsq/p/4715447.html

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