HDU 1131

N个节点的不同的树的数目。这样

随便取一个节点作为根,那么他左边和右边的儿子节点个数就确定了,假定根节点标号为x,那么左子树的标号就从1到x-1,共x-1个,右子树的标号就从x+1到n,共n-x个,那么我们的x从1取到n,就获得了所有的情况数

这是一个递推的式子,初始值与卡特兰数的初值相同。所以,解正是卡特兰数。又由于节点有序,所以乘上N!。

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Scanner;
import java.io.InputStreamReader;

class Cont{
	BigDecimal []num;
	Cont(){
		num=new BigDecimal[110];
		num[0]=new BigDecimal(1);
		BigDecimal Tmp;
		for(int i=1;i<=100;i++){
			Tmp=new BigDecimal(i);
			num[i]=num[i-1].multiply(Tmp);
		}
	}
}

public class Main{
    public static void main(String args[]){
        Scanner in=new Scanner(System.in);
        BigDecimal []Can=new BigDecimal[110];
        Can[0]=new BigDecimal(1);
        BigDecimal B,C,D;
        Cont Conmul=new Cont();
        for(int i=1;i<=100;i++){
            B=new BigDecimal(4*i-2);
            C=new BigDecimal(i+1);
            D=Can[i-1].multiply(B);
            Can[i]=D.divide(C);
        }
        while(in.hasNext()){
	int x=in.nextInt();
	if(x==0) break;
	BigDecimal ans=new BigDecimal(1);
	ans=ans.multiply(Can[x]);
	ans=ans.multiply(Conmul.num[x]);
	System.out.println(ans);
        }
    }
}

  

时间: 2024-11-05 19:42:36

HDU 1131的相关文章

UVa 10007 &amp; hdu 1131 Count the Trees (卡特兰数)

Count the Trees Time Limit:3000MS    Memory Limit:0KB     64bit IO Format:%lld & %llu SubmitStatus Description  Count the Trees  Another common social inability is known as ACM (Abnormally Compulsive Meditation). This psychological disorder is somewh

HDU 1131 Count the Trees 大数计算

题目是说给出一个数字,然后以1到这个数为序号当做二叉树的结点,问总共有几种组成二叉树的方式.这个题就是用卡特兰数算出个数,然后因为有编号,不同的编号对应不同的方式,所以结果是卡特兰数乘这个数的阶乘种方案.因为数字比较大,所以要用高精度的方法也就是用字符数组来做,我分别写了三个函数,一个算加法,一个算乘法,最后一个打表,等打出表来最后只要判断一下输入的数是第几个,直接输出就行了,下面是我的代码,第一次写高精度的这种大数处理,可能看上去比较繁琐= = #include<iostream> #inc

hdu 1131 Count the Trees

卡特兰数*阶乘就是答案     第一次用java..高精度写起来好快....不过代码比较丑.. import java.math.BigInteger; import java.util.Scanner; public class Main { public static void main(String []args) { Scanner cin = new Scanner(System.in); int i,ii; BigInteger []c= new BigInteger[105]; B

HDU分类

模拟题, 枚举 1002 1004 1013 1015 1017 1020 1022 1029 1031 1033 1034 1035 1036 1037 1039 1042 1047 1048 1049 1050 1057 1062 1063 1064 1070 1073 1075 1082 1083 1084 1088 1106 1107 1113 1117 1119 1128 1129 1144 1148 1157 1161 1170 1172 1177 1197 1200 1201 12

转载:hdu 题目分类 (侵删)

转载:from http://blog.csdn.net/qq_28236309/article/details/47818349 基础题:1000.1001.1004.1005.1008.1012.1013.1014.1017.1019.1021.1028.1029. 1032.1037.1040.1048.1056.1058.1061.1070.1076.1089.1090.1091.1092.1093. 1094.1095.1096.1097.1098.1106.1108.1157.116

HDU——PKU题目分类

HDU 模拟题, 枚举1002 1004 1013 1015 1017 1020 1022 1029 1031 1033 1034 1035 1036 1037 1039 1042 1047 1048 1049 1050 1057 1062 1063 1064 1070 1073 1075 1082 1083 1084 1088 1106 1107 1113 1117 1119 1128 1129 1144 1148 1157 1161 1170 1172 1177 1197 1200 1201

HDU——1130 How Many Trees?

How Many Trees? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 3952    Accepted Submission(s): 2238 Problem Description A binary search tree is a binary tree with root k such that any node v re

Hdu 4311-Meeting point-1 曼哈顿距离,前缀和

题目:http://acm.hdu.edu.cn/showproblem.php?pid=4311 Meeting point-1 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3426    Accepted Submission(s): 1131 Problem Description It has been ten years s

[最小表示法] hdu 2609 How many

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2609 How many Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1225    Accepted Submission(s): 476 Problem Description Give you n ( n < 10000) nec