D. How many trees?

题目链接:http://codeforces.com/problemset/problem/9/D

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.

Examples

Input
3 2
Output
5
Input
3 3
Output
4

题目大意: 用n个点组成平衡二叉树,问高度大于等于h的有多少个

思路:看到这道题,感觉有无数种可能啊,而且树的形态太多了,并不会求,下面是整体思路

首先题目说的是二叉树,所以对于一个节点来说与他有关的就是它的左子树和右子树如果知道子树的所有可能和右子树的所有可能的,那么我们乘起来就好了

dp[i][j]表示使用i个结点 深度为j的总个数 要求这个值的话,我们需要从左右子树下手

使用i个结点,深度为j,我们枚举左子树和右子树的结点个数,假设左子树的个数为L,那么右子树的个数为i-L-1(j减去根)

对于一颗节点为L的子树来讲,我们枚举她可能拥有的所有深度,乘以右子树可能拥有的所有深度,答案就出来了

 其实一直在想为什么这样子可以求出答案,首先节点数是递增的,我们要求i节点 ,比i小的一定已经求出来了

 因为题目与深度有关,所有我们必须考虑深度之间的转移,当前节点的深度就是左右子树大的那个深度+1,符合最优子结构关系

 所以是可行的

看代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<stack>
#include<map>
#include<queue>
#include<cmath>
using namespace std;
typedef long long LL;
typedef unsigned long long ull;
#define sc1(a) scanf("%lld",&a)
#define pf1(a) printf("%lld
",a)
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
const LL INF=1e18;
const ull base=2333;
const int maxn=1e2+50;
const int maxm=1e2+50;
const int maxv=1e6+5;
const int mod=1e9+7;
const int ba=3e5;
LL dp[maxn][maxn];//dp[i][j]表示用了i个结点 深度为j的方案数
int main()
{
    LL N,H;sc1(N);sc1(H);
    //我们只要枚举左右子树的所以可能性就可以了
    dp[0][0]=1;//0个结点可以构成1颗深度为0的树
    for(int i=1;i<=N;i++) //当前的总节点数
    {
        for(int j=0;j<i;j++) //枚举左子树的总个数
        {
            LL r=i-j-1;//所以右子树的总个数就是i-左子树-根
            //知道左右子树的总个数 我们还需要枚举左右子树的深度
                for(int k=0;k<=j;k++) //左子树的深度
                {
                    for(int l=0;l<=r;l++) //右子树的深度
                    {
                        dp[i][max(k,l)+1]+=dp[j][k]*dp[r][l];
                    }
                }
        }
    }
    LL ans=0;
    for(int i=H;i<=N;i++) ans+=dp[N][i];
    pf1(ans);
    return 0;
}
/**

*/
当初的梦想实现了吗,事到如今只好放弃吗~
原文地址:https://www.cnblogs.com/caijiaming/p/12327767.html