Codeforces-830D Singer House(组合数+dp)
It is known that passages in Singer house are complex and intertwined. Let's define a Singer k-house as a graph built by the following process: take complete binary tree of height k and add edges from each vertex to all its successors, if they are not yet present.
Count the number of non-empty paths in Singer k-house which do not pass the same vertex twice. Two paths are distinct if the sets or the orders of visited vertices are different. Since the answer can be large, output it modulo 109 + 7.
The only line contains single integer k (1 ≤ k ≤ 400).
Print single integer — the answer for the task modulo 109 + 7.
2
9
3
245
20
550384565
There are 9 paths in the first example (the vertices are numbered on the picture below): 1, 2, 3, 1-2, 2-1, 1-3, 3-1, 2-1-3, 3-1-2.
设f[i][j]为i-house中有j条路径的方案数,则答案就是f[n][1]
状态转移自然是从i-1转移到i
设i的2个子树中的路径数为f[i-1][j]和f[i-1][k],转移方案为:
①不选择根节点:f[i][j+k]=f[i-1][j]*f[i-1][k]
②选择根节点与某一路径结合:f[i][j+k]=f[i-1][j]*f[i-1][k]*(j+k)*2
③选择根节点将左右两子树的各一条路径结合:f[i][j+k-1]=f[i-1][j]*f[i-1][k]*C(j+k,2)*2
④选择根节点独自成为一条路径:f[i][j+k+1]=f[i-1][j]*f[i-1][k]
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int MX = 405;
const LL mod = 1e9 + 7;
const int INF = 0x3f3f3f3f;
LL f[MX][MX];
int main(void) {
//freopen("in.txt", "r", stdin);
int n;
scanf("%d", &n);
f[1][1] = f[1][0] = 1;
for (int i = 2; i <= n; i++) {
for (int j = 0; j <= n; j++) {
for (int k = 0; k + j <= n; k++) {
LL t = f[i - 1][j] * f[i - 1][k] % mod;
(f[i][j + k] += t % mod * ((j + k) * 2 + 1)) %= mod;
(f[i][j + k + 1] += t) %= mod;
(f[i][j + k - 1] += t * (j + k) * (j + k - 1)) %= mod;
}
}
}
printf("%I64d\n", f[n][1]);
return 0;
}