更好的阅读体验

Portal

Portal1: Codeforces

Portal2: Luogu

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 \le 35, h \le n)\).

Output

Output one number - the answer to the problem. It is guaranteed that it does not exceed \(9 \times 10^18\).

Sample Input1

3 2

Sample Output1

5

Sample Input2

3 3

Sample Output2

4

Description in Chinese

用\(n\)个点组成二叉树,问高度大于等于\(h\)的有多少个。

Solution

这题\(n\)的范围很小,只有\(35\),所以我们用\(O(n^4)\)的动态规划来解决。

dp[i][j]表示用\(i\)个点,构成高度为\(j\)的二叉树的个数。

动态转移方程为\(dp[i][max(j, k) + 1] += dp[L][j] \times dp[R][k]\),其中\(i\)表示所用的结点数,\(j\)是枚举左子树的结点数,\(L\)表示左子树的结点总数,\(k\)是枚举左子树的结点数,\(R\)表示右子树的结点总数。

答案就是\(\sum^{n}_{i = h}{dp[n][i]}\)。

Code

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath> using namespace std; typedef long long LL;
const int MAXN = 40;
int n, h;
LL dp[MAXN][MAXN];
int main() {
scanf("%d%d", &n, &h);
dp[0][0] = 1;//用0个结点,可以构造出1棵深度为0的二叉树
for (int i = 1; i <= n; i++)//枚举总结点数
for (int L = 0; L < i; L++) {//枚举左子树的结点数
int R = i - L - 1;//计算剩下的结点数,再减去根结点,就是右子树的结点数
for (int j = 0; j <= L; j++)//左子树
for (int k = 0; k <= R; k++)//右子树
dp[i][max(j, k) + 1] += dp[L][j] * dp[R][k];//因为是累计可行方案所以是乘法
}
LL ans = 0;
for (int i = h; i <= n; i++)
ans += dp[n][i];//累计答案
printf("%lld\n", ans);
return 0;
}

『题解』Codeforces9D How many trees?的更多相关文章

  1. 『题解』洛谷P1063 能量项链

    原文地址 Problem Portal Portal1:Luogu Portal2:LibreOJ Portal3:Vijos Description 在\(Mars\)星球上,每个\(Mars\)人 ...

  2. 『题解』Codeforces1142A The Beatles

    更好的阅读体验 Portal Portal1: Codeforces Portal2: Luogu Description Recently a Golden Circle of Beetlovers ...

  3. 『题解』Codeforces1142B Lynyrd Skynyrd

    更好的阅读体验 Portal Portal1: Codeforces Portal2: Luogu Description Recently Lynyrd and Skynyrd went to a ...

  4. 『题解』洛谷P1993 小K的农场

    更好的阅读体验 Portal Portal1: Luogu Description 小\(K\)在\(\mathrm MC\)里面建立很多很多的农场,总共\(n\)个,以至于他自己都忘记了每个农场中种 ...

  5. 『题解』洛谷P2296 寻找道路

    更好的阅读体验 Portal Portal1: Luogu Portal2: LibreOJ Description 在有向图\(\mathrm G\)中,每条边的长度均为\(1\),现给定起点和终点 ...

  6. 『题解』洛谷P1351 联合权值

    更好的阅读体验 Portal Portal1: Luogu Portal2: LibreOJ Description 无向连通图\(\mathrm G\)有\(n\)个点,\(n - 1\)条边.点从 ...

  7. 『题解』Codeforces656E Out of Controls

    更好的阅读体验 Portal Portal1: Codeforces Portal2: Luogu Description You are given a complete undirected gr ...

  8. 『题解』洛谷P2170 选学霸

    更好的阅读体验 Portal Portal1: Luogu Description 老师想从\(N\)名学生中选\(M\)人当学霸,但有\(K\)对人实力相当,如果实力相当的人中,一部分被选上,另一部 ...

  9. 『题解』洛谷P1083 借教室

    更好的阅读体验 Portal Portal1: Luogu Portal2: LibreOJ Portal3: Vijos Description 在大学期间,经常需要租借教室.大到院系举办活动,小到 ...

随机推荐

  1. idea中的java web项目(添加jar包介绍)和java maven web项目目录结构

    java web项目 web项目下web根目录名称是可以更改的 idea中新建java web项目,默认src为Sources Root,当然也可以手动改,在Sources Root下右键只能新建Pa ...

  2. 第二篇 python进阶

    目录 第二篇 python进阶 一 数字类型内置方法 二 字符串类型内置方法 三 列表类型内置方法(list) 四 元组类型内置方法(tuple) 五 字典内置方法 六 集合类型内置方法(self) ...

  3. 从输入URL到页面渲染完成 -戈多编程

    1.输入URL地址 2.浏览器根据域名查询IP地址 3.浏览器发送HTTP请求到web服务器 4.服务器返回一个永久重定向响应 5.浏览器会跟踪重定向地址 6.服务器处理请求 7.服务器返回一个HTM ...

  4. 将自定义功能添加到Spring Data Repository

    Spring Data非常方便,可以加快开发速度,避免使用样板代码. 但是,在某些情况下,注释查询不足,而无法达到您可能希望实现的自定义功能. 因此,Spring Data允许我们向Spring Da ...

  5. springboot 快速开发的定制补充

    增强 SpringBoot 快速开发工具 项目地址:https://gitee.com/sanri/web-ui 优点:这是一个 web 通用配置的组件,即插即用,可用于新项目或私活.是对 Sprin ...

  6. ESP8266开发之旅 进阶篇② 闲聊Arduino IDE For ESP8266烧录配置

    授人以鱼不如授人以渔,目的不是为了教会你具体项目开发,而是学会学习的能力.希望大家分享给你周边需要的朋友或者同学,说不定大神成长之路有博哥的奠基石... QQ技术互动交流群:ESP8266&3 ...

  7. V2er - Best client for V2EX

    V2er - Best client for V2EX 可能是体验最好的掌上 V2EX 客户端,专为 iOS 打造并在 Github 开源. 关于 V2EX,V2EX 是创意工作者们的社区.这里目前汇 ...

  8. electron快捷键

    我们分为在主进程中注册快捷键和在渲染进程中注册快捷键 在主进程中我们有两种方式 一 利用[Menu]来模拟快捷键,只有app获得焦点时才生效,很少使用 const { Menu, MenuItem } ...

  9. 在react项目中使用redux or mobx?

    主要比较参数: 库体积,打包项目体积 开发体验 性能对比 在对比参数前首先分析一下redux和mobx的设计模式,redux和mobx都没有使用传统的mvc/mvvm形式,而且他们使用flux结构也略 ...

  10. 面试又被 Java 基础难住了?推荐你看看这篇文章。

    本文已经收录自 JavaGuide (59k+ Star):[Java学习+面试指南] 一份涵盖大部分Java程序员所需要掌握的核心知识. 1. 面向对象和面向过程的区别 面向过程 :面向过程性能比面 ...