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 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 Input
3 2
Sample Output
5
Hint
题意
问你一个二叉树,有n个节点,深度大于等于h的一共有多少种
这个二叉树满足左儿子比自己小,右儿子比自己大的特性。
题解:
dp,dp[i][j]表示当前用了i个节点,高度小于等于j的方案数
dp[i][j] = sigma(dp[k][j-1]*dp[i-k-1][j-1])
枚举左子树和右子树
然后莽一波就好了
代码
#include<bits/stdc++.h>
using namespace std;
const int maxn = 37;
long long dp[maxn][maxn];
int main()
{
int n,h;
scanf("%d%d",&n,&h);
for(int i=1;i<=n;i++)
{
dp[0][i-1]=1;
for(int j=1;j<=n;j++)
for(int k=0;k<j;k++)
dp[j][i]+=dp[k][i-1]*dp[j-k-1][i-1];
}
cout<<dp[n][n]-dp[n][h-1]<<endl;
}
Codeforces Beta Round #9 (Div. 2 Only) D. How many trees? dp的更多相关文章
- Codeforces Beta Round #4 (Div. 2 Only) B. Before an Exam dp
B. Before an Exam 题目连接: http://www.codeforces.com/contest/4/problem/B Description Tomorrow Peter has ...
- Codeforces Beta Round #80 (Div. 2 Only)【ABCD】
Codeforces Beta Round #80 (Div. 2 Only) A Blackjack1 题意 一共52张扑克,A代表1或者11,2-10表示自己的数字,其他都表示10 现在你已经有一 ...
- Codeforces Beta Round #83 (Div. 1 Only)题解【ABCD】
Codeforces Beta Round #83 (Div. 1 Only) A. Dorm Water Supply 题意 给你一个n点m边的图,保证每个点的入度和出度最多为1 如果这个点入度为0 ...
- Codeforces Beta Round #79 (Div. 2 Only)
Codeforces Beta Round #79 (Div. 2 Only) http://codeforces.com/contest/102 A #include<bits/stdc++. ...
- Codeforces Beta Round #77 (Div. 2 Only)
Codeforces Beta Round #77 (Div. 2 Only) http://codeforces.com/contest/96 A #include<bits/stdc++.h ...
- Codeforces Beta Round #76 (Div. 2 Only)
Codeforces Beta Round #76 (Div. 2 Only) http://codeforces.com/contest/94 A #include<bits/stdc++.h ...
- Codeforces Beta Round #75 (Div. 2 Only)
Codeforces Beta Round #75 (Div. 2 Only) http://codeforces.com/contest/92 A #include<iostream> ...
- Codeforces Beta Round #74 (Div. 2 Only)
Codeforces Beta Round #74 (Div. 2 Only) http://codeforces.com/contest/90 A #include<iostream> ...
- Codeforces Beta Round #73 (Div. 2 Only)
Codeforces Beta Round #73 (Div. 2 Only) http://codeforces.com/contest/88 A 模拟 #include<bits/stdc+ ...
随机推荐
- Scrum冲刺博客(总)
一.在 Alpha 阶段认领的任务 二.明日任务安排 成员 任务 任务量/小时 陈伟泽 项目进度把控 1 李嘉廉 24点算法" Stack数据结构实现,生成不重复的算式(考虑结合律和结合律) ...
- 服务器大量TIME_WAIT和CLOSE_WAIT的原因及解决办法
Linux服务器下查看网络连接的状态 netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}' 它会显示例如下面的信息: ...
- JavaBean和Map的相互转换
JavaBean和Map的相互转换 一.JavaBean 1.什么是JavaBean? JavaBean其实就是一种遵循特定写法的类,必须遵循一定的规范: 类必须由public修饰,并且保证有公共的无 ...
- C语言使用pthread多线程编程(windows系统)一
运行之前需要做一些配置: 1.下载PTHREAD的WINDOWS开发包 pthreads-w32-2-4-0-release.exe(任何一个版本均可) http://sourceware.or ...
- 详解hdparm: linux下的硬盘测速工具
hdparm的功能:显示与设定硬盘的参数.hdparm可检测,显示与设定IDE或SCSI硬盘的参数. 语法: hdparm [-CfghiIqtTvyYZ][-a <快取分区>][-A & ...
- Mybatis新增mysql时中文乱码
Mybatis新增mysql时中文乱码 1.设置数据库连接的编码(jdbc.properties) jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:my ...
- 纯php实现中秋博饼游戏(2):掷骰子并输出结果
这篇是纯php实现中秋博饼游戏系列博文(2) 上文是:纯php实现中秋博饼游戏(1):绘制骰子图案 http://www.cnblogs.com/zqifa/p/php-dice-1.html要纯ph ...
- mysql 统计一个列不同值的数量
SELECT count(status = 0 OR NULL) AS a, count(status = 1 OR NULL) AS b, count(status = 2 OR NULL) AS ...
- 教你判断一个APP页面是原生的还是H5页面 。(还没看)
来源:https://www.25xt.com/appdesign/11851.html 刚好是周末,无意之间学堂君在收集相关资料的时候,发现有部分童鞋在问<如何判断一个APP页面是不是H5页面 ...
- [转][html]大文件下载
上面代码来自微软,用于下载大文件. 下面代码来自 http://www.cnblogs.com/smile-wei/p/4159213.html System.IO.Stream iStream = ...