Codeforces 830D Singer House - 动态规划

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.

Example

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): 1, 2, 3, 1-2, 2-1, 1-3, 3-1, 2-1-3, 3-1-2.

 Singer 2-house


  题目大意 取一棵深度为k的完全二叉树,每个点向他的所有祖父连一条双向边,问新图中存在多少条有向简单路径。

  显然动态规划(不然你还能组合数乱搞?)

  考虑用f[i][j]表示在i-house中,叶节点的j级祖先作为路径起点的有向简单路径的条数。然后发现一个点从它某个祖先绕回来绕回子树的方案数很难统计。

  然后我就不会做了跑去看题解,题解用f[i][j]表示在i-house中,选取了j条会不想交的简单有向路径的方案数。合并的时候枚举两棵(i - 1)-house中的路径数,考虑几种情况:

  1)什么都不操作,将方案数累加到f[i][j + k]的头上

  2)加入i级祖先然后什么都不做,将方案数累加到f[i][j + k + 1]的上

  3)通过i级祖先连接一条路径,因为可以从i级祖先连接这条路径的起点或从它的终点到i级祖先,所以将方案数乘2累加到f[i][j + k + 1]上。

  4)通过i级祖先合并任意两条路径(一定是可能的,因为 每个低于i级点到i级祖先都有直接的边相连),j + k条路径中选两条的方案数乘方案数再乘2累加到f[i][j + k - 1]上。

  (ps:以上的方案数指的是从左子树选择j条路径的方案数乘从右子树选择k条路径的方案数)

Code

 1 /**
 2  * Codeforces
 3  * Problem#830C
 4  * Accepted
 5  * Time: 1247ms
 6  * Memory: 600k
 7  */
 8 #include <bits/stdc++.h>
 9 using namespace std;
10 #define smplus(a, b) a = (a + b) % moder
11
12 const int moder = 1e9 + 7;
13 const int lim = 405;
14
15 int n;
16 int f[lim][lim];
17
18 inline void init() {
19     scanf("%d", &n);
20 }
21
22 inline int C(int a) {
23     return a * (a - 1);
24 }
25
26 inline void solve() {
27     f[1][0] = f[1][1] = 1;
28     for(int i = 2; i <= n; i++)
29         for(int j = 0; j <= n; j++)
30             for(int k = 0; k <= n - j; k++) {
31                 int t = (long long)f[i - 1][j] * f[i - 1][k] % moder;
32                 smplus(f[i][j + k + 1], t);        //把i级祖先当成一个单独的点加入
33                 smplus(f[i][j + k], t * 1LL * (((j + k) << 1) + 1) % moder);    //不加入i级祖先或者连通一条路径和i级祖先
34                 if(j + k)
35                     smplus(f[i][j + k - 1], t * 1LL * C(j + k));            //从j + k条路径中选择2条通过i级祖先合并
36             }
37     printf("%d", f[n][1]);
38 }
39
40 int main() {
41     init();
42     solve();
43     return 0;
44 }

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.

Example

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): 1, 2, 3, 1-2, 2-1, 1-3, 3-1, 2-1-3, 3-1-2.

 Singer 2-house

时间: 2024-10-11 07:12:45

Codeforces 830D Singer House - 动态规划的相关文章

Singer House CodeForces - 830D (组合计数,dp)

大意: 一个$k$层完全二叉树, 每个节点向它祖先连边, 就得到一个$k$房子, 求$k$房子的所有简单路径数. $DP$好题. 首先设$dp_{i,j}$表示$i$房子, 分出$j$条简单路径的方案数, 那么最终答案就为$dp_{i,1}$. 考虑两棵$i-1$房子转移到$i$房子的情况, 分四种情况. 两个子树间不与根节点连边, 那么$dp_{i,j+k}=\sum dp_{i-1,j}dp_{i-1,k}$ 两个子树只有一条路径与根节点连边, $dp_{i,j+k}=\sum dp_{i-

Codeforces 834D The Bakery - 动态规划 - 线段树

Some time ago Slastyona the Sweetmaid decided to open her own bakery! She bought required ingredients and a wonder-oven which can bake several types of cakes, and opened the bakery. Soon the expenses started to overcome the income, so Slastyona decid

Codeforces 837D Round Subset - 动态规划 - 数论

Let's call the roundness of the number the number of zeros to which it ends. You have an array of n numbers. You need to choose a subset of exactly k numbers so that the roundness of the product of the selected numbers will be maximum possible. Input

Codeforces 1000G Two-Paths 树形动态规划 LCA

原文链接https://www.cnblogs.com/zhouzhendong/p/9246484.html 题目传送门 - Codeforces 1000G Two-Paths 题意 给定一棵有 $n(2\leq n\leq 3\times 10^5)$ 个节点的树,其中节点 $i$ 有权值 $a_i$,边 $e$ 有权值 $w_e$.$(1\leq a_i,w_e\leq 10^9)$ 现在给出 $q(1\leq q\leq 4\times 10^5)$ 组询问,每组询问给定两个数 $x,

CodeForces 429B Working out 动态规划

Description Summer is coming! It's time for Iahub and Iahubina to work out, as they both want to look hot at the beach. The gym where they go is a matrix a with n lines and m columns. Let number a[i][j] represents the calories burned by performing wo

【动态规划】Codeforces 699C Vacations

题目链接: http://codeforces.com/problemset/problem/699/C 题目大意: N天,A(健身)或B(做比赛)或休息,每天都有4种情况,A可行B可行,A可行B不行,A不行B可行,AB都不行. 每天选择一种,不能连续两天选择同一种活动(可以连续休息),问最少休息几天. 题目思路: [动态规划] f[i][j]表示前i天,最后一天状态为j的最多休息天数(最少天数也行),j=0,1,2表示休息,运动和做比赛. 转移方程挺好推的. 1 // 2 //by coolx

Codeforces Round #251 (Div. 2) A - Devu, the Singer and Churu, the Joker

水题 #include <iostream> #include <vector> #include <algorithm> using namespace std; int main(){ int n,d,t; cin >> n >> d; for(int i = 0 ; i < n; ++ i){ cin >> t; d-=t; } d-=(n-1)*10; if(d < 0) cout<<-1<<

动态规划,就是这样! CodeForces 433B - Kuriyama Mirai&#39;s Stones

Kuriyama Mirai has killed many monsters and got many (namely n) stones. She numbers the stones from 1 to n. The cost of the i-th stone is vi. Kuriyama Mirai wants to know something about these stones so she will ask you two kinds of questions: She wi

Codeforces 506E Mr. Kitayuta&#39;s Gift (矩阵乘法,动态规划)

描述: 给出一个单词,在单词中插入若干字符使其为回文串,求回文串的个数(|s|<=200,n<=10^9) 这道题超神奇,不可多得的一道好题 首先可以搞出一个dp[l][r][i]表示回文串左边i位匹配到第l位,右边i位匹配到第r位的状态数,可以发现可以用矩阵乘法优化(某人说看到n这么大就一定是矩阵乘法了= =) 但这样一共有|s|^2个节点,时间复杂度无法承受 我们先把状态树画出来:例如add 可以发现是个DAG 我们考虑把单独的每条链拿出来求解,那么最多会有|s|条不同的链,链长最多为|s