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.
InputThe 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
题目大意:输入n代表高度为n的完全二叉树,将每一个节点都和自己的祖父连接一条无向边,求可以有多少条不相交的简单路径。
解题思路是构造DP,先理解简单路径,就是一条路径上,除了起点和终点外,其他的顶点都不相同。
令dp[i][j] 代表高度为i的有j条简单路径的方案数。
仔细分析可以从高度是i-1得到递推式子,根据完全二叉树的性质,我们可以将一棵高度是i的看成是两棵高度是i-1的完全二叉树合并到一个顶点而成的,因此可以分为左右子树的关系想:
枚举左子树dp[i-1][j]和右子树dp[i-1][k],显然最终的答案就是dp[n][1](高度是n,只有一条简单路径的所有方案数),因此按情况分析如下:
1、dp[i][j+k] += dp[i-1][j] * dp[i-1][k] (可以理解为高度是i的有j+k条简单路径的方案数要加上不将根节点连接在内的,左右子树不进行操作的方案数,为什么是乘的,因为左边是j时,右边是k的所有方案数都是合法的。)
2、dp[i][j+k] += dp[i-1][j] * dp[i-1][k] * C(j+k,1)* 2 (可以这样理解,高度是i的有j+k的简单路径的方案数要加上根节点连左子树(或者右子树,所以乘2)中任一条简单路径(所以是C(j+k,1)))
3、dp[i][j+k-1] += dp[i-1][j] * dp[i-1][k] * C(j+k,2)* 2 (可以这样理解,高度是i的有j+k-1条简单路径的方案数要加上:根节点连接左和右子树中的各取出一条简单路径(所以合并后的图中有(k-1)+(j-1)+1 == k+j-1条简单路径)的方案数)
4、dp[i][j+k+1] += dp[i-1][j] * dp[i-1][k] (可以这样理解,高度是i的有j+k+1条简单路径的方案数要加上:这时候根节点独自成为一条简单路径,因此合并后的图中就有左子树的j,右子树的k,和根节点的1条,一共有(j+k+1)条简单路的方案数。)
具体代码实现如下:(最后记得取MOD)
如果有问题欢迎提出来,也欢迎提出错误。