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的更多相关文章

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

  2. Codeforces Beta Round #80 (Div. 2 Only)【ABCD】

    Codeforces Beta Round #80 (Div. 2 Only) A Blackjack1 题意 一共52张扑克,A代表1或者11,2-10表示自己的数字,其他都表示10 现在你已经有一 ...

  3. Codeforces Beta Round #83 (Div. 1 Only)题解【ABCD】

    Codeforces Beta Round #83 (Div. 1 Only) A. Dorm Water Supply 题意 给你一个n点m边的图,保证每个点的入度和出度最多为1 如果这个点入度为0 ...

  4. Codeforces Beta Round #79 (Div. 2 Only)

    Codeforces Beta Round #79 (Div. 2 Only) http://codeforces.com/contest/102 A #include<bits/stdc++. ...

  5. Codeforces Beta Round #77 (Div. 2 Only)

    Codeforces Beta Round #77 (Div. 2 Only) http://codeforces.com/contest/96 A #include<bits/stdc++.h ...

  6. Codeforces Beta Round #76 (Div. 2 Only)

    Codeforces Beta Round #76 (Div. 2 Only) http://codeforces.com/contest/94 A #include<bits/stdc++.h ...

  7. Codeforces Beta Round #75 (Div. 2 Only)

    Codeforces Beta Round #75 (Div. 2 Only) http://codeforces.com/contest/92 A #include<iostream> ...

  8. Codeforces Beta Round #74 (Div. 2 Only)

    Codeforces Beta Round #74 (Div. 2 Only) http://codeforces.com/contest/90 A #include<iostream> ...

  9. Codeforces Beta Round #73 (Div. 2 Only)

    Codeforces Beta Round #73 (Div. 2 Only) http://codeforces.com/contest/88 A 模拟 #include<bits/stdc+ ...

随机推荐

  1. Alpha阶段第1周 Scrum立会报告+燃尽图 06

    作业要求与https://edu.cnblogs.com/campus/nenu/2018fall/homework/2246相同 一.小组介绍 组长:刘莹莹 组员:朱珅莹 孙韦男 祝玮琦 王玉潘 周 ...

  2. Alpha冲刺一 (6/10)

    前言 队名:拖鞋旅游队 组长博客:https://www.cnblogs.com/Sulumer/p/10004107.html 作业博客:https://edu.cnblogs.com/campus ...

  3. linux内存查看工具

    这里帮你总结了一下Linux下查看内存使用情况的多种方法~ 在做 Linux 系统优化的时候,物理内存是其中最重要的一方面.自然的,Linux 也提供了非常多的方法来监控宝贵的内存资源的使用情况.下面 ...

  4. 三十分钟理解:双调排序Bitonic Sort,适合并行计算的排序算法

    欢迎转载,转载请注明:本文出自Bin的专栏blog.csdn.net/xbinworld 技术交流QQ群:433250724,欢迎对算法.技术.应用感兴趣的同学加入 双调排序是data-indepen ...

  5. 用 PHPMailer 发送邮件

    REFs http://gohom.win/2015/07/02/PHPmailer/ http://blog.wpjam.com/m/phpmailer/ https://www.kancloud. ...

  6. zabbix利用python脚本发送钉钉报警

    #!/usr/bin/python # -*- coding: utf-8 -*- import requests import json import sys import os headers = ...

  7. tensorflow中协调器 tf.train.Coordinator 和入队线程启动器 tf.train.start_queue_runners

    TensorFlow的Session对象是支持多线程的,可以在同一个会话(Session)中创建多个线程,并行执行.在Session中的所有线程都必须能被同步终止,异常必须能被正确捕获并报告,会话终止 ...

  8. alsa-lib、alsa-utils移植

    /************************************************************************** * alsa-lib.alsa-utils移植 ...

  9. nats 学习 集群ha 配置

      nats 的ha 是一个mesh 的结构,有两个主要的参数 clusters routers 启动三分节点(单机) 共享变量 SERVERS=nats://127.0.0.1:6222,nats: ...

  10. Erlang process structure -- refc binary

    Erlang 的process 是虚拟机层面的进程,每个Erlang process 都包括一个 pcb(process control block), 一个stack 以及私有heap . 这部分的 ...