D. How many trees?

time limit per test

1 second

memory limit per test

64 megabytes

input

standard input

output

standard output

In one very old text file there was written Great Wisdom. This Wisdom was so Great that nobody could decipher it, even Phong — the oldest among the inhabitants of Mainframe. But still he managed to get some information from there. For example, he managed to learn that User launches games for pleasure — and then terrible Game Cubes fall down on the city, bringing death to those modules, who cannot win the game...

For sure, as guard Bob appeared in Mainframe many modules stopped fearing Game Cubes. Because Bob (as he is alive yet) has never been defeated by User, and he always meddles with Game Cubes, because he is programmed to this.

However, unpleasant situations can happen, when a Game Cube falls down on Lost Angles. Because there lives a nasty virus — Hexadecimal, who is... mmm... very strange. And she likes to play very much. So, willy-nilly, Bob has to play with her first, and then with User.

This time Hexadecimal invented the following entertainment: Bob has to leap over binary search trees with n nodes. We should remind you that a binary search tree is a binary tree, each node has a distinct key, for each node the following is true: the left sub-tree of a node contains only nodes with keys less than the node's key, the right sub-tree of a node contains only nodes with keys greater than the node's key. All the keys are different positive integer numbers from 1 to n. Each node of such a tree can have up to two children, or have no children at all (in the case when a node is a leaf).

In Hexadecimal's game all the trees are different, but the height of each is not lower than h. In this problem «height» stands for the maximum amount of nodes on the way from the root to the remotest leaf, the root node and the leaf itself included. When Bob leaps over a tree, it disappears. Bob gets the access to a Cube, when there are no trees left. He knows how many trees he will have to leap over in the worst case. And you?

Input

The input data contains two space-separated positive integer numbers n and h (n ≤ 35, h ≤ n).

Output

Output one number — the answer to the problem. It is guaranteed that it does not exceed 9·1018.

Sample test(s)
input
3 2
output
5
input
3 3
output
4

题解:(我网上看到的)最后的状态是什么?也就是说求的是什么? N个节点h>=H的二差索引树的种数。能不能简化一下,因为这样直接求每次要判断h >= H ? 很难处理的,而且容易漏。想一想高度大于H的数可不可以通过某些数据相减得到?哦,可以这样表述:N个节点高度不大于N的树的种数-N个节点高度不大于H-1的种数就是题目所求的。很好!那么有没有什么想法?没有。再想一下最后的结果和那些状态有关? 节点数N和高度h。很好!那么节点数为N高度为h的树的种数是怎么得到的?是通过两棵子树种数的乘积得到的。能不能再具体一些,稍微用一下数学式子表示一下? Dp[k][n] = dp[~-k][i] * dp[~-k][n - i - 1] , 其中k是高度,n是节点数,i是左儿子的节点数,n - i - 1 是右儿子的节点数。非常好,你的思路是完全正确的。那么能不能再考虑一下初始条件是什么?...打个比方说:dp[0][0] = ? dp[0][1] = ? dp[1][0] = ? ... Oh!dp[0][i] = 0 , ( 1 <= i <= n )  dp[i][0] = 1 ( 0 <= i <= n ) , 很好,现在可以写代码了.
 #include <iostream>
#include <string.h>
using namespace std;
long long dp[][];
int main()
{
int n,h,i,j,k;
cin>>n>>h;
memset(dp,,sizeof(dp));
for(i=; i<=n; i++)
dp[i][]=;
for(i=; i<=n; i++)
{
for(j=; j<=n; j++)
{
for(k=; k<j; k++)
{
dp[i][j]+=dp[i-][k]*dp[i-][j-k-];
}cout<<dp[i][j]<<" ";
}cout<<endl;
}
cout<<dp[n][n]-dp[h-][n]<<endl;
}
 

D. How many trees? DP的更多相关文章

  1. Codeforces Round #369 (Div. 2) C. Coloring Trees DP

    C. Coloring Trees   ZS the Coder and Chris the Baboon has arrived at Udayland! They walked in the pa ...

  2. CodeForces #369 C. Coloring Trees DP

    题目链接:C. Coloring Trees 题意:给出n棵树的颜色,有些树被染了,有些没有.现在让你把没被染色的树染色.使得beauty = k.问,最少使用的颜料是多少.   K:连续的颜色为一组 ...

  3. C. Coloring Trees DP

    传送门:http://codeforces.com/problemset/problem/711/C 题目: C. Coloring Trees time limit per test 2 secon ...

  4. codeforces 711C C. Coloring Trees(dp)

    题目链接: C. Coloring Trees time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

  5. BC.5200.Trees(dp)

    Trees  Accepts: 156  Submissions: 533  Time Limit: 2000/1000 MS (Java/Others)  Memory Limit: 65536/6 ...

  6. Codeforces 677C. Coloring Trees dp

    C. Coloring Trees time limit per test:2 seconds memory limit per test:256 megabytes input:standard i ...

  7. Codeforces Beta Round #9 (Div. 2 Only) D. How many trees? dp

    D. How many trees? 题目连接: http://www.codeforces.com/contest/9/problem/D Description In one very old t ...

  8. CF 9D. How many trees?(dp)

    题目链接 以前做过类似的,USACO,2.3,开始数组开小了,导致数据乱了,然后超数据范围了,.. #include <cstdio> #include <iostream> ...

  9. Codeforces 711 C. Coloring Trees (dp)

    题目链接:http://codeforces.com/problemset/problem/711/C 给你n棵树,m种颜色,k是指定最后的完美值.接下来一行n个数 表示1~n树原本的颜色,0的话就是 ...

随机推荐

  1. 分布式测试工具Beetle.DT的部署并进行HTTP,SQL,TCP压测

    由于Beetle.DT是一个分布式压力测试工具,所以在使用上并不像普通工具那样安装运行这么简单:由于工具涉及到测试管理中心,节点和管理端等工具: 所以必须要进行相应的部署才能运行.接下来详解一下如果安 ...

  2. java.lang.OutOfMemoryError 解决程序启动内存溢出问题

    java.lang.OutOfMemoryError: Java heap space Myeclipse里面部署的java web项目,浏览器访问的时候出现错误: type Exception re ...

  3. 四,ESP8266 TCP服务器

    我要赶时间赶紧写完所有的内容....朋友的东西答应的还没做完呢!!!!!!!没想到又来了新的事情,,....... 配置模块作为TCP服务器然后呢咱们连接服务器发指令控制继电器吸合和断开 控制的指令呢 ...

  4. bootstrap导航条+模态对话框+分页样式

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  5. 团队作业4——第一次项目冲刺 fOURth DaY

    项目冲刺--Quadra Kill 兄弟们,再坚持一下,再坚持一下,再给我一个头我就五杀了. 今天可谓是项目的一个转折点,因为跳转和数据库已经基本写好啦,鼓掌~[啪啪啪啪啪啪] 让我们来看看今天大家做 ...

  6. 团队作业9--beta版本测试报告及发布说明

    Beta版本测试报告 1.bug的分类 a.修复的bug 部分用户无法获取位置 e. 这个bug的确应该修复,但是没有时间在这个版本修复,延迟到下一个版本修复. 前端无法查看用户签到信息 2.场景测试 ...

  7. 团队作业8——第二次项目冲刺(Beta阶段)Day4--5.21

    展开圆桌式会议: 会议内容:1.团队成员对昨天任务完成情况做一个简单交流,并对昨天工作中存在的问题提出集中讨论解决:2.按照昨天的昨天工作分配表做具体的任务分配:3.简单讨论明天的任务分配每个人的工作 ...

  8. 201521123026 《Java程序设计》第5周学习总结

    1. 本章学习总结 尝试使用思维导图总结有关多态与接口的知识点 使用常规方法总结其他上课内容 1.接口的出现时为了实现多态,多态的实现不一定依赖于接口. 2.接口的常见成员有:全局常量和抽象方法. 3 ...

  9. 201521123036 《Java程序设计》第4周学习总结

    本周学习总结 1.1 尝试使用思维导图总结有关继承的知识点. 1.2 使用常规方法总结其他上课内容. 继承:extends子类将获得父类的属性与方法,并具有自身特有的属性与方法 抽取共同特征(行为与属 ...

  10. 201521123038 《Java程序设计》 第三周学习总结

    201521123038 <Java程序设计> 第三周学习总结 1. 本周学习总结 附大图链接 http://naotu.baidu.com/file/5774caa2be710afbc0 ...