[CF9D] How many trees? - dp
Description
用 n 个点组成二叉树,问高度大于等于 h 的有多少个。(h le n le 35)
Solution
大于的不大好求,我们考虑求小于等于
设 (f[i][j]) 表示用 i 个点组成高度小于等于 j 的二叉树,有多少棵
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 45;
int f[N][N], n, h;
signed main()
{
ios::sync_with_stdio(false);
cin >> n >> h;
for (int i = 0; i <= n; i++)
f[0][i] = 1, f[1][i] = i > 0;
for (int i = 2; i <= n; i++)
for (int j = 1; j <= n; j++)
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;
}