欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Codeforces-830D Singer House(组合数+dp)

程序员文章站 2022-07-12 12:30:59
...

D. Singer House
time limit per test
2 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

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.

Codeforces-830D Singer House(组合数+dp)Singer 4-house

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.

Input

The only line contains single integer k (1 ≤ k ≤ 400).

Output

Print single integer — the answer for the task modulo 109 + 7.

Examples
input
2
output
9
input
3
output
245
input
20
output
550384565
Note

There are 9 paths in the first example (the vertices are numbered on the picture below): 1231-22-11-33-12-1-33-1-2.

Codeforces-830D Singer House(组合数+dp)Singer 2-house

设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;
}