HDU——1130 How Many Trees?
How Many Trees?Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) 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
|
|||
|
题目大意:
给定一个数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?的更多相关文章
- hdu 1130 How Many Trees?(Catalan数)
How Many Trees? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- HDU 1130 How Many Trees?
裸的卡特兰数 C++#include<iostream> #include<cstdio> using namespace std; #define base 10000 #d ...
- hdu 1130 How Many Trees? 【卡特兰数】
题目 题意:给你一个数字n,问你将1~n这n个数字,可以组成多少棵不同的二叉搜索树. 1,2,5,14--根据输出中的规律可以看出这是一个卡特兰数的序列.于是代用卡特兰数中的一个递推式: 因为输入可取 ...
- HDU ACM 1134 Game of Connections / 1130 How Many Trees?(卡特兰数)
[题目链接]http://acm.hdu.edu.cn/showproblem.php?pid=1134 [解题背景]这题不会做,自己推公式推了一段时间,将n=3和n=4的情况列出来了,只发现第n项与 ...
- hdu 1693 Eat the Trees——插头DP
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1693 第一道插头 DP ! 直接用二进制数表示状态即可. #include<cstdio> # ...
- hdu 1130,hdu 1131(卡特兰数,大数)
How Many Trees? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- HDU 1392 Surround the Trees(凸包入门)
Surround the Trees Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- HDU - 1392 Surround the Trees (凸包)
Surround the Trees:http://acm.hdu.edu.cn/showproblem.php?pid=1392 题意: 在给定点中找到凸包,计算这个凸包的周长. 思路: 这道题找出 ...
- 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 ...
随机推荐
- [SDOI2009]学校食堂
题目描述 小F 的学校在城市的一个偏僻角落,所有学生都只好在学校吃饭.学校有一个食堂,虽然简陋,但食堂大厨总能做出让同学们满意的菜肴.当然,不同的人口味也不一定相同,但每个人的口味都可以用一个非负整数 ...
- ACM_01背包
背包1 Time Limit: 2000/1000ms (Java/Others) Problem Description: 有n个重量和价值分别为Wi,Vi的物品,现从这些物品中挑选出总量不超过 W ...
- Kerberos 简介——教你做个好人
文章导读: 对称加密 非对称加密 数字证书 Kerberos认证流程 Hadoop生态利用Kerberos认证机制来识别可靠的服务和节点,保障Hadoop集群的安全,那么Kerberos到底是什么?为 ...
- CSV文件转EXCEl(java)
package main; import java.io.BufferedReader;import java.io.DataInputStream;import java.io.File;impor ...
- PAT甲级考前整理(2019年3月备考)之一
转载请注明出处:https://www.cnblogs.com/jlyg/p/7525244.html 终于在考前,刷完PAT甲级131道题目,不容易!!!每天沉迷在刷题之中而不能超脱,也是一种 ...
- PHP7 上传文件报错 Internal Server Error 解决方法
打开Apache配置httpd.conf.在最后添加FcgidMaxRequestLen指令一个足够大的值(以字节为单位),例如 FcgidMaxRequestLen 100000000 最后重新启动 ...
- Codeforces_776_C_(思维)(前缀和)
C. Molly's Chemicals time limit per test 2.5 seconds memory limit per test 512 megabytes input stand ...
- TCP端口状态LISTENING ESTABLISHED CLOSE_WAIT TIME_WAIT SYN_SENT
TCP状态转移要点 TCP协议规定,对于已经建立的连接,网络双方要进行四次握手才能成功断开连接,如果缺少了其中某个步骤,将会使连接处于假死状态,连接本身占用的资源不 会被释放.网络服务器程序要同时管理 ...
- django在验证登录页面时遇到的数据查询问题
数据库查询时针对不存在的用户名进行验证 django在查询数据库时,可以使用get和filter两种方法. 两者的区别 当数据库内不存在该数据时,get会返回异常,而filter会返回空. 当数据库内 ...
- 【Redis】二、Redis高级特性
(三) Redis高级特性 前面我们介绍了Redis的五种基本的数据类型,灵活运用这五种数据类型是使用Redis的基础,除此之外,Redis还有一些特性,掌握这些特性能对Redis有进一步的了解, ...