Question:
Given N deep
1. Construct a weighted tree with N nodes with a maximum depth of deep. Each node can have up to two sons.
2. The value of each node is 2 ^ 0, 2 ^ 1 · 2 ^ (n-1). Any two node values cannot be the same.
3. For a node, if he has a left and right son, then the sum of the Left subtree and the sum of the right subtree
Q:
Number of constructor methods.
Ideas:
DP
#include <stdio.h>#include <iostream>#include <algorithm>#include <cstring>using namespace std;template <class T>inline bool rd(T &ret) { char c; int sgn; if(c=getchar(),c==EOF) return 0; while(c!='-'&&(c<'0'||c>'9')) c=getchar(); sgn=(c=='-')?-1:1; ret=(c=='-')?0:(c-'0'); while(c=getchar(),c>='0'&&c<='9') ret=ret*10+(c-'0'); ret*=sgn; return 1;}template <class T>inline void pt(T x) { if (x <0) { putchar('-'); x = -x; } if(x>9) pt(x/10); putchar(x%10+'0');}///////////////////////////////////typedef long long ll;const int N = 362;const ll mod = 1000000000 + 7;int d[N][N], p[N][N], c[N][N];int main() {memset(d, 0, sizeof d);memset(p, 0, sizeof p);memset(c, 0, sizeof c);for (int i = 0; i < N; ++i) {c[i][0] = c[i][i] = 1;for (int j = 1; j < i; ++j)c[i][j] = (c[i - 1][j] + c[i-1][j-1]) % mod;}for (int i = 0; i < N; ++i)p[0][i] = 1;for (int i = 1; i < N; ++i)p[1][i] = 1;for (int i = 2; i < N; ++i)for (int j = 1; j < N; ++j) {p[i][j] += (ll)p[i-1][j-1] * 2 % mod;p[i][j] %= mod;if (i - 1 >= 2) {for (int k = 1; k <= i - 2; ++k) {p[i][j] += ((ll)c[i-2][k] * p[k][j-1] % mod) * p[i-1-k][j-1] % mod;p[i][j] %= mod;}}p[i][j] = (ll)p[i][j] * i % mod;}//int x, y; while (~scanf("%d%d", &x, &y)) printf("%d\n", p[x][y]);d[1][1] = 1; d[0][0] = 1;for (int i = 2; i < N; ++i)for (int j = i; j < N; ++j) {d[i][j] += (ll)2 * d[i-1][j-1] % mod;d[i][j] %= mod;if (j - 1 >= 2) {for (int k = 1; k <= j - 2; ++k) {d[i][j] += ((ll)c[j-2][k] * p[k][i-2] % mod) * d[i-1][j-1-k] % mod;d[i][j] %= mod;d[i][j] += ((ll)c[j-2][k] * d[i-1][k] % mod) * p[j-1-k][i-2] % mod;d[i][j] %= mod;d[i][j] += ((ll)c[j-2][k] * d[i-1][k] % mod) * d[i-1][j-1-k] % mod;d[i][j] %= mod;}}d[i][j] = (ll)d[i][j] * j % mod;}int T = 0, cas, u, v;scanf("%d", &cas);while (cas -- > 0) {rd(u); rd(v);printf("Case #");pt(++T);putchar(':');putchar(' ');pt(d[v][u]);putchar('\n');}return 0;}
HDU 4359 easy tree DP? Constructor DP