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 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:  1131 1134 1133 1023 2067

Statistic | Submit | Discuss | Note

Home | Top Hangzhou Dianzi University Online Judge 3.0
Copyright © 2005-2017 HDU ACM Team. All Rights Reserved.
Designer & DeveloperWang Rongtao LinLe GaoJie GanLu
Total 0.015600(s) query 5, Server time : 2017-08-10 07:32:01, Gzip enabled
Administration

题目大意:

给定一个数n,你能说出有多少个不同的二进制搜索树可以用一组n个大小的数字来构造,这样集合的每个元素都将与二进制搜索树中的一个节点的标签相关联?

思路:

看题意,准是有很多大佬已经想到这个题就是让求一个二叉树有几种形态,那不就是一个裸的卡特兰数吗?!

然后我就信心满满的写了个卡特兰数,结果交上去接着就wa了。。。。

靠,卡特兰数到52就暴long long了!!!

唉,要用高精啊。。。。

代码:

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define N 110
using namespace std;
int n,len,ans,sum,tmp,a[N][N],b[N];
int catelan()
{
    len=1; a[1][0]=b[1]=1;
    for(int i=2;i<110;i++)
    {
        for(int j=0;j<len;j++)
         a[i][j]=a[i-1][j]*(4*i-2);
        sum=0;
        for(int j=0;j<len;j++)
        {
            tmp=sum+a[i][j];
            a[i][j]=tmp%10;
            sum=tmp/10;
        }
        while(sum)
        {
            a[i][len++]=sum%10;
            sum/=10;
        }
        for(int j=len-1;j>=0;j--)
        {
            tmp=sum*10+a[i][j];
            a[i][j]=tmp/(i+1);
            sum=tmp%(i+1);
        }
        while(!a[i][len-1])
         len--;
        b[i]=len;
    }
}
int main()
{
    catelan();
    while(~scanf("%d",&n))
    {
        for(int i=b[n]-1;i>=0;i--)
         printf("%d",a[n][i]);
        printf("\n");
    }
    return 0;
}
时间: 2024-12-28 11:12:33

HDU——1130 How Many Trees?的相关文章

hdu 1392 Surround the Trees (凸包)

Surround the Trees Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 7043    Accepted Submission(s): 2688 Problem Description There are a lot of trees in an area. A peasant wants to buy a rope to

hdu 1693 Eat the Trees (插头dp入门)

Eat the Trees Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2507    Accepted Submission(s): 1225 Problem Description Most of us know that in the game called DotA(Defense of the Ancient), Pudg

HDU 1392 Surround the Trees

PS: 在求解两个点的时候就是两个点的距离,在这WA了一次. #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <vector> #include <cmath> using namespace std; const int maxn = 110; struct point { int x, y; point(d

hdu 1392 Surround the Trees(凸包果题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1392 Surround the Trees Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7473    Accepted Submission(s): 2860 Problem Description There are a lot o

HDU 1693 Eat the Trees 插头DP

链接:http://acm.hdu.edu.cn/showproblem.php?pid=1693 题意:给出一块r*c的地,(r,c<=11),其中有的土地上种上了树,有些没有种上树,只能在种上树的地上走,通过走若干个回路,来走遍所有种树的土地.问有多少种走法. 思路:无论如何还是要先提供一个链接:http://wenku.baidu.com/view/4fe4ac659b6648d7c1c74633.html,是国家队论文,里面对于要提及的概念讲解的十分清楚. dp的过程是从左上角的点到右下

【插头DP】HDU 1693 Eat the Trees

通道:http://acm.hdu.edu.cn/showproblem.php?pid=1693 题意:多回路路径方案数,无障碍. 代码: 1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 5 using namespace std; 6 7 const int MAX_N = 13; 8 const int MAX_M = 13; 9 const int HASH = 10007; 1

HDU - 1392 Surround the Trees (凸包)

Surround the Trees:http://acm.hdu.edu.cn/showproblem.php?pid=1392 题意: 在给定点中找到凸包,计算这个凸包的周长. 思路: 这道题找出凸包上的点后,s数组中就是按顺序的点,累加一下距离就是周长了. #include <algorithm> #include <iterator> #include <iostream> #include <cstring> #include <cstdli

HDU 1392 Surround the Trees (凸包周长)

题目链接:HDU 1392 Problem Description There are a lot of trees in an area. A peasant wants to buy a rope to surround all these trees. So at first he must know the minimal required length of the rope. However, he does not know how to calculate it. Can you

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