hdoj-1130-How Many Trees?【卡特兰数】

How Many Trees?

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

Total Submission(s): 3224 Accepted Submission(s): 1870

Problem Description

A binary search tree is a binary tree with root k such that any node v reachable from its left has label (v) <label (k) and any node w reachable from its right has label (w) > label (k). It is a search structure which can find a node
with label x in O(n log n) average time, where n is the size of the tree (number of vertices).

Given a number n, can you tell how many different binary search trees may be constructed with a set of numbers of size n such that each element of the set will be associated to the label of exactly one node in a binary search tree?

Input

The input will contain a number 1 <= i <= 100 per line representing the number of elements of the set.

Output

You have to print a line in the output for each entry with the answer to the previous question.

Sample Input

1
2
3

Sample Output

1
2
5

Source

UVA

Recommend

Eddy | We have carefully selected several similar problems for you:
1133 2067 1267 1100 1124

#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));
	for(i=2;i<=100;++i){
		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];
		}
	}

}
int main(){
	make();
	int n;
	while(~scanf("%d",&n)){
	    int i=maxn-1;
	    while(catalan[n][i]==0) --i;
	    for(;i>=0;--i){
	    	printf("%d",catalan[n][i]);
	    }
	    printf("\n");
	}
	return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-21 11:28:33

hdoj-1130-How Many Trees?【卡特兰数】的相关文章

FZU 1775 Counting Binary Trees 卡特兰数前n项和%m(m可为非素数

题目链接:点击打开链接 题意: 卡特兰数前n项和 结果%m 把答案当成2部分搞. #include<stdio.h> #include<cmath> #define int __int64 const int N = 100000; struct inverse_element{ int x, y, q; void extend_Eulid(int a,int b) { if(b == 0){ x = 1;y = 0;q = a; }else{ extend_Eulid(b,a%b

hdu 1130How Many Trees?(卡特兰数)

卡特兰数又称卡塔兰数,英文名Catalan number,是组合数学中一个常出现在各种计数问题中出现的数列. 以比利时的数学家欧仁·查理·卡塔兰 (1814–1894)的名字来命名,其前几项为(从第零项开始) : 1, 1, 2, 5, 14, 42, 132, 429, 1430, 4862, 16796, 58786, 208012, 742900, 2674440, 9694845, 35357670, 129644790, 477638700, 1767263190, 656412042

hdoj-1023-Train Problem II【卡特兰数】

Train Problem II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6852 Accepted Submission(s): 3708 Problem Description As we all know the Train Problem I, the boss of the Ignatius Train Station wa

卡特兰数,高精度卡特兰数

简介:卡特兰数是组合数学中经常出现的一个数列. 个人觉得无论是递推公式还是代表的含义都比斐波那契数列难理解一些. 递推公式: 应用: 1.Cn表示长度2n的dyck word的个数.Dyck word是一个有n个X和n个Y组成的字串,且所有的前缀字串皆满足X的个数大于等于Y的个数.以下为长度为6的dyck words XXXYYY XYXXYY XYXYXY XXYYXY XXYXYY 分析:对于n个X,n个Y的排列来说一共有C(n,2n)种,还要排除其中不符合要求的排列.不符合条件的排列对于某

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 1130(卡特兰数,大数)

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

HDOJ 5184 Brackets 卡特兰数扩展

既求从点(0,0)只能向上或者向右并且不穿越y=x到达点(a,b)有多少总走法... 有公式: C(a+b,min(a,b))-C(a+b,min(a,b)-1)  /// 折纸法证明卡特兰数: http://blog.sina.com.cn/s/blog_6917f47301010cno.html Brackets Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total

hdoj 4828 卡特兰数取模

Grids Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) Total Submission(s): 93    Accepted Submission(s): 25 Problem Description 度度熊近期非常喜欢玩游戏.这一天他在纸上画了一个2行N列的长方形格子. 他想把1到2N这些数依次放进去.可是为了使格子看起来优美,他想找到使每行每列都递增的方案.只是画了

HDOJ/HDU 1133 Buy the Ticket(数论~卡特兰数~大数~)

Problem Description The "Harry Potter and the Goblet of Fire" will be on show in the next few days. As a crazy fan of Harry Potter, you will go to the cinema and have the first sight, won't you? Suppose the cinema only has one ticket-office and

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 cl