CodeForces 9D How many trees?

2988 단어
제목: 너에게 두 갈래 나무가 하나 있는데 n개의 노드가 있고 깊이가 h보다 큰 것은 모두 몇 가지가 있느냐고 묻는다. 이 두 갈래 나무는 왼쪽 아들이 자신보다 작고 오른쪽 아들이 자신보다 큰 특성을 만족시킨다.
사고방식: dp[i][j]는 현재 i개 노드를 사용했고 높이는 j와 같은 방안수보다 적다는 것을 나타낸다
#include<bits/stdc++.h>
using namespace std;
#define LL long long
LL dp[40][40];
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];
   }
   printf("%lld
",dp[n][n]-dp[n][h-1]); }

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
Input
3 2

Output
5

Input
3 3

Output
4

좋은 웹페이지 즐겨찾기