频道栏目
首页 > 资讯 > 其他 > 正文

D.How many trees?(dp)节点问题求解

17-12-23        来源:[db:作者]  
收藏   我要投稿

Description

n个节点构成的高度不小于h的二叉树数量

Input

两个整数n,h(1≤h≤n≤35)

Output

输出n个节点构成的高度不小于h的二叉树数量

Sample Input

3 2

Sample Output

5

Solution

dp[n][h]表示n个节点构成的高度为h的二叉树数量,枚举左儿子节点数量x和高度h1以及右儿子高度h2,则右儿子节点数量为n?1?x,进而有转移方程dp[n][h]=∑h1,h2,xdp[x][h1]?dp[n?1?x][h2],记忆化一下即可

Code

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef pairP;
const int maxn=40;
bool flag[maxn][maxn];
ull dp[maxn][maxn];
ull Solve(int n,int h)
{
    if(flag[n][h])return dp[n][h];
    flag[n][h]=1;
    if(n==h)
    {
        if(n==0)return dp[n][h]=1;
        return dp[n][h]=1ull<<(n-1);
    }
    if(((1ll<<><>
        
   
<>
相关TAG标签
上一篇:C.Digital Root(数论)求解
下一篇:vue学习(一):环境搭建
相关文章
图文推荐

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站