这题我想了好久

设 \(f_{i,j}\) 为 \(i\) 结点 \(<=j\) 的方案数

固定根,枚举左右子树,就有:

\[f_{i,j}=\sum_{k=0}^{n-1}f_{k,j-1}*f_{i-k-1,j-1}
\]

初始化 \(f_{0,i}=1\)

答案 \(ans=f_{n,n}-f_{n,h-1}\)

Code

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#define N 40
#define int long long
using namespace std;
inline int read() {
int x=0,f=1; char ch=getchar();
while(ch<'0' || ch>'9') { if(ch=='-') f=-1; ch=getchar(); }
while(ch>='0'&&ch<='9') { x=(x<<3)+(x<<1)+(ch^48); ch=getchar(); }
return x * f;
}
int n,h;
int f[N][N]; //f[i][j] 表示 i个结点 高度为j 的方案总数
signed main()
{
n = read(), h = read();
for(int i=0;i<=n;++i)
f[0][i] = 1;
for(int j=1;j<=n;++j)
for(int i=1;i<=n;++i)
for(int k=0;k<i;++k)
f[i][j] += f[k][j-1]*f[i-k-1][j-1];
cout<<f[n][n]-f[n][h-1]<<endl;
return 0;
}

CF9D 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. D. How many trees? DP

    D. How many trees? time limit per test 1 second memory limit per test 64 megabytes input standard in ...

  7. Codeforces 677C. Coloring Trees dp

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

  8. 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 ...

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

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

随机推荐

  1. SQL 批量修改一个字段的值为另一个字段的值

    AND create_time BETWEEN '2016-07-25 14:30:00' AND '2016-07-25 15:20:28'; AND create_time BETWEEN '20 ...

  2. IDEA中代码不小心删除,或者改了半天想回退到某个特定时间怎么办?

    第一步: 第二步: 第三步: 第四步:

  3. fengmiantu3

  4. php 判断访问是否是手机或者pc

    php代码 function isMobile() { $user_agent = $_SERVER['HTTP_USER_AGENT']; $mobile_agents = Array(" ...

  5. 微信小程序 button 组件

    button 组件 拥有强大的功能 自身可以拥有很多跟微信风格的样式,且是 表单 和 开放的能力 重要的 按钮 button 的属性: size: 类型 字符串 按钮的大小 属性值:default 默 ...

  6. Android animation summary

    Android animation 动画定义 动画的意思就是一连串画面动起来了,根据这一连串画面的产生原理可分为两类:补间动画(Tween animation)和帧动画(frame animation ...

  7. a special kind of crossword called a word square

    w Language Log: Wordplay Watch #2: The hunt for the ten-square  http://itre.cis.upenn.edu/~myl/langu ...

  8. rm命令反向选择删除文件

    反向删除文件, 参考这篇文章. http://blog.csdn.net/web_go_run/article/details/46009723 shopt是设置shell的全局选项 shopt -p ...

  9. 像计算机科学家一样思考python-第3章 函数

    在程序设计中,函数是指用于进行某种计算的一系列语句的有名称的组合.定义一个函数时,需要指定函数的名称并写下一系列程序语句.之后,就可以使用名称来“调用”这个函数 3.1函数调用 一个函数调用的例子 & ...

  10. 阶段1 语言基础+高级_1-3-Java语言高级_04-集合_01 Collection集合_6_迭代器的实现原理