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

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.

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.

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



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值