Codeforces Beta Round #9 (Div. 2 Only) D. How many trees? dp

题目链接:

http://www.codeforces.com/contest/9/problem/D

D. How many trees?

time limit per test1 second
memory limit per test64 megabytes
#### 问题描述 > 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? #### 输入 > The input data contains two space-separated positive integer numbers n and h (n ≤ 35, h ≤ n). #### 输出 > 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

题意

问你节点数为n,树高>=h的二叉搜索树有多少种

代码

#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<ctime>
#include<vector>
#include<cstdio>
#include<string>
#include<bitset>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
using namespace std;
#define X first
#define Y second
#define mkp make_pair
#define lson (o<<1)
#define rson ((o<<1)|1)
#define mid (l+(r-l)/2)
#define sz() size()
#define pb(v) push_back(v)
#define all(o) (o).begin(),(o).end()
#define clr(a,v) memset(a,v,sizeof(a))
#define bug(a) cout<<#a<<" = "<<a<<endl
#define rep(i,a,b) for(int i=a;i<(b);i++)
#define scf scanf
#define prf printf

typedef __int64 LL;
typedef vector<int> VI;
typedef pair<int,int> PII;
typedef vector<pair<int,int> > VPII;

const int INF=0x3f3f3f3f;
const LL INFL=0x3f3f3f3f3f3f3f3fLL;
const double eps=1e-8;
const double PI = acos(-1.0);

//start----------------------------------------------------------------------

const int maxn=44;

//dp[i][j]表示节点数为i,高度至少为j的搜索二叉树有多少种。
//dp2[i]表示节点数为i的二叉树有多少颗。
LL dp[maxn][maxn],dp2[maxn];

int n,h;

int main() {

    clr(dp2,0);
    dp2[0]=dp2[1]=1;

    for(int i=2;i<maxn;i++){
        //枚举根节点
        for(int k=1;k<=i;k++){
            dp2[i]+=dp2[k-1]*dp2[i-k];
        }
    }

    clr(dp,0);
    dp[0][0]=1;
    for(int i=1;i<maxn;i++){
        for(int j=0;j<maxn;j++){
            if(j<=1){
                dp[i][j]=dp2[i];
                continue;
            }
            //枚举根节点为k
            for(int k=1;k<=i;k++){
                //这里容斥一下
                dp[i][j]+=dp2[k-1]*dp[i-k][j-1];
                dp[i][j]+=dp[k-1][j-1]*dp2[i-k];
                dp[i][j]-=dp[k-1][j-1]*dp[i-k][j-1];
            }
        }
    }

    scf("%d%d",&n,&h);
    prf("%I64d\n",dp[n][h]);
    return 0;
}

//end-----------------------------------------------------------------------
posted @ 2016-09-05 01:42  fenicnn  阅读(243)  评论(0编辑  收藏  举报