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
 
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=; a[][]=b[]=;
    ;i<;i++)
    {
        ;j<len;j++)
         a[i][j]=a[i-][j]*(*i-);
        sum=;
        ;j<len;j++)
        {
            tmp=sum+a[i][j];
            a[i][j]=tmp%;
            sum=tmp/;
        }
        while(sum)
        {
            a[i][len++]=sum%;
            sum/=;
        }
        ;j>=;j--)
        {
            tmp=sum*+a[i][j];
            a[i][j]=tmp/(i+);
            sum=tmp%(i+);
        }
        ])
         len--;
        b[i]=len;
    }
}
int main()
{
    catelan();
    while(~scanf("%d",&n))
    {
        ;i>=;i--)
         printf("%d",a[n][i]);
        printf("\n");
    }
    ;
}

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

  1. hdu 1130 How Many Trees?(Catalan数)

    How Many Trees? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  2. HDU 1130 How Many Trees?

    裸的卡特兰数 C++#include<iostream> #include<cstdio> using namespace std; #define base 10000 #d ...

  3. hdu 1130 How Many Trees? 【卡特兰数】

    题目 题意:给你一个数字n,问你将1~n这n个数字,可以组成多少棵不同的二叉搜索树. 1,2,5,14--根据输出中的规律可以看出这是一个卡特兰数的序列.于是代用卡特兰数中的一个递推式: 因为输入可取 ...

  4. HDU ACM 1134 Game of Connections / 1130 How Many Trees?(卡特兰数)

    [题目链接]http://acm.hdu.edu.cn/showproblem.php?pid=1134 [解题背景]这题不会做,自己推公式推了一段时间,将n=3和n=4的情况列出来了,只发现第n项与 ...

  5. hdu 1693 Eat the Trees——插头DP

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1693 第一道插头 DP ! 直接用二进制数表示状态即可. #include<cstdio> # ...

  6. hdu 1130,hdu 1131(卡特兰数,大数)

    How Many Trees? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  7. HDU 1392 Surround the Trees(凸包入门)

    Surround the Trees Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  8. HDU - 1392 Surround the Trees (凸包)

    Surround the Trees:http://acm.hdu.edu.cn/showproblem.php?pid=1392 题意: 在给定点中找到凸包,计算这个凸包的周长. 思路: 这道题找出 ...

  9. 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 ...

随机推荐

  1. iOS检测耳机插入拔出

    首先,需要导入两个框架 然后,注册通知检测耳机的插入与拔出操作 [[NSNotificationCenter defaultCenter] addObserver:self selector:@sel ...

  2. [ USACO 2017 FEB ] Why Did the Cow Cross the Road III (Gold)

    \(\\\) \(Description\) 给定长度为\(2N\)的序列,\(1\text ~N\)各出现过\(2\)次,\(i\)第一次出现位置记为\(a_i\),第二次记为\(b_i\),求满足 ...

  3. get、post、put、delete、head请求方式

    对资源的增,删,改,查操作,其实都可以通过GET/POST完成,不一定要用PUT和DELETE. 一:Jersey框架,实现了restful风格,常用的注解@GET.@POST.@PUT.@DELET ...

  4. Mantis 配置与使用学习

    转载自:http://blog.csdn.net/xifeijian/article/category/1429687

  5. Filesystem Hierarchy Standard (Unix, Linux etc)

    http://www.pathname.com/fhs/ /boot -- Static files of the boot loader Purpose: contains everything r ...

  6. 梦想CAD控件安卓控件事件

    MxDrawActivity.commandEvent 命令调用事件. 参数 说明 int iCommand 命令ID,这个ID用户自已来取的,只要多个命令ID不重复就可以 代码实现如下: publi ...

  7. Java集合(一)--Comparable和Comparator

    Comparable: 是集合内部的方法实现的排序,只有一个方法 public interface Comparable<T> { public int compareTo(T o); } ...

  8. centos7安装个人博客wordpress

    第一步: 安装apache web 第二步: 安装MariaDB数据库 第三步: 安装PHP 第四步: 安装phpMyAdmin 第五部: 创建数据库: 第六部: 下载wordpress 第七部:复制 ...

  9. 全局唯一的支付和订单id生成算法

    数据库存储的是两个Long类型的复合主键.显示到页面的是一个27位的数字单号 package com.yunyihenkey.common.idworker; /** * * @desc * @aut ...

  10. python bs4库

    Beautiful Soup parses anything you give it, and does the tree traversal stuff for you. BeautifulSoup ...