传送门:http://codeforces.com/problemset/problem/9/D
【题解】
树形dp,f(i,j)表示i个节点,高度为j的方案数,枚举左子树大小和哪一个子树高度为j-1即可。
不加任何优化时间复杂度$O(n^4)$
1 # include <bits/stdc++.h> 2 using namespace std; 3 4 typedef long long ll; 5 const int M = 50; 6 7 int n, h; 8 ll f[M][M]; 9 10 int main() { 11 cin >> n >> h; 12 f[0][0] = 1; 13 for (int i=1; i<=n; ++i) 14 for (int j=1; j<=i; ++j) { 15 for (int k=0; k<=i-1; ++k) 16 for (int l=0; l<=j-1; ++l) 17 f[i][j] += f[k][j-1]*f[i-k-1][l]; 18 for (int k=0; k<=i-1; ++k) 19 for (int l=0; l<=j-2; ++l) 20 f[i][j] += f[k][j-1]*f[i-k-1][l]; 21 } 22 ll ans = 0; 23 for (int i=h; i<=n; ++i) ans += f[n][i]; 24 cout << ans; 25 return 0; 26 }