大意: 一个$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-1,j}dp_{i-1,k} 2(j+k)$
  • 两个子树有两条路径与根节点连边, $dp_{i,j+k-1}=\sum dp_{i-1,j}dp_{i-1,k} (j+k)(j+k-1)$
  • 两个子树间不与根节点连边, 根节点单独作为一条路径, $dp_{i,j+k+1}=\sum dp_{i-1,j}dp_{i-1,k}$
#include <iostream>
#include <sstream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <cstring>
#include <bitset>
#include <functional>
#include <random>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define hr putchar(10)
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '\n'
#define DB(a) ({REP(__i,1,n) cout<<a[__i]<<',';hr;})
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int P = 1e9+7, INF = 0x3f3f3f3f;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
inline int rd() {int x=0;char p=getchar();while(p<'0'||p>'9')p=getchar();while(p>='0'&&p<='9')x=x*10+p-'0',p=getchar();return x;}
//head const int N = 510;
int n, dp[N][N];
void add(int &x, ll y) {x=(x+y)%P;}
int main() {
scanf("%d", &n);
dp[1][1] = dp[1][0] = 1;
REP(i,2,n) {
REP(j,0,n) if (dp[i-1][j]) {
REP(k,0,n-j) if (dp[i-1][k]) {
ll t = (ll)dp[i-1][j]*dp[i-1][k]%P;
add(dp[i][j+k],t);
add(dp[i][j+k],t*2*(j+k));
if (j+k) add(dp[i][j+k-1],t*(j+k)*(j+k-1));
add(dp[i][j+k+1],t);
}
}
}
printf("%d\n", dp[n][1]);
}

Singer House CodeForces - 830D (组合计数,dp)的更多相关文章

  1. [ZJOI2010]排列计数 (组合计数/dp)

    [ZJOI2010]排列计数 题目描述 称一个1,2,...,N的排列P1,P2...,Pn是Magic的,当且仅当2<=i<=N时,Pi>Pi/2. 计算1,2,...N的排列中有 ...

  2. BZOJ1079 [SCOI2008]着色方案[组合计数DP]

    $有a_{1}个1,a_{2}个2,...,a_{n}个n(n<=15,a_{n}<=5),求排成一列相邻位不相同的方案数.$ 关于这题的教训记录: 学会对于复杂的影响分开计,善于发现整体 ...

  3. Codeforces 176B【计数DP】

    题意: 给你两个串s1,s2和一个K, 有一种操作是在一个串切开然后交换位置, 问s1有多少种方法经过K次这样的操作变成s2: 思路: (从来没接触过计数DP...还是太菜...参考了[大牛blog] ...

  4. Yet Another Problem On a Subsequence CodeForces - 1000D (组合计数)

    大意:定义一个长为$k>1$且首项为$k-1$的区间为好区间. 定义一个能划分为若干个好区间的序列为好序列. 给定序列$a$, 求有多少个子序列为好序列. 刚开始一直没想出来怎么避免重复计数, ...

  5. luoguP4492 [HAOI2018]苹果树 组合计数 + dp

    首先,每个二叉树对应着唯一的中序遍历,并且每个二叉树的概率是相同的 这十分的有用 考虑\(dp\)求解 令\(f_i\)表示\(i\)个节点的子树,根的深度为\(1\)时,所有点的期望深度之和(乘\( ...

  6. Intercity Travelling CodeForces - 1009E (组合计数)

    大意: 有一段$n$千米的路, 每一次走$1$千米, 每走完一次可以休息一次, 每连续走$x$次, 消耗$a[1]+...+a[x]$的能量. 休息随机, 求消耗能量的期望$\times 2^{n-1 ...

  7. hdu4779 组合计数+dp

    提交 题意:给了n*m的网格,然后有p个重型的防御塔,能承受1次攻击,q个轻型防御塔不能接受任何攻击,然后每个防御搭会攻击他所在的行和所在的列,最后求在这个网格上放至少一个防御塔的方案数, 我们枚举 ...

  8. Anton and School - 2 CodeForces - 785D (组合计数,括号匹配)

    大意: 给定括号字符串, 求多少个子序列是RSGS. RSGS定义如下: It is not empty (that is n ≠ 0). The length of the sequence is ...

  9. Different Subsets For All Tuples CodeForces - 660E (组合计数)

    大意: 定义$f(a)$表示序列$a$本质不同子序列个数. 给定$n,m$, 求所有长$n$元素范围$[1,m]$的序列的$f$值之和. 显然长度相同的子序列贡献是相同的. 不考虑空串, 假设长$x$ ...

随机推荐

  1. Nginx-实践篇(重要)

    原文链接:https://blog.csdn.net/Fe_cow/article/details/84672361

  2. TP5 查询 字符串条件如何实现

      TP5 查询 字符串条件如何实现 当查询条件是 (1,3,8) ,3,4) 这种情况改如何查询呢?   主要用到FIND_IN_SET $where[ ]=>['exp',Db::raw(& ...

  3. PrivateIpAddresses Array of String 实例主网卡的内网IP列表。 PublicIpAddresses Array of String 实例主网卡的公网IP列表。 注意:此字段可能返回 null,表示取不到有效值。

    https://cloud.tencent.com/document/api/213/15753 浮动 IP 地址 https://cloud.google.com/solutions/best-pr ...

  4. TypeScript的泛型接口 泛型类接口

    /* typeScript中的泛型 泛型接口 */ //函数类型接口 /* interface ConfigFn{ (value1:string,value2:string):string; } va ...

  5. shell编程系列13--文本处理三剑客之sed利用sed追加文件内容

    shell编程系列13--文本处理三剑客之sed利用sed追加文件内容 追加用法总结: .a 在匹配行后面追加 .i 在匹配行前面追加 .r 将文件内容追加到匹配行后面 .w 将匹配行写入指定文件 追 ...

  6. osg编译日志3

    1>------ 已启动生成: 项目: ZERO_CHECK, 配置: Debug x64 ------1> Checking Build System1> CMake does n ...

  7. *438. Find All Anagrams in a String 找到字符串中所有字母异位词

    1. 原始题目 给定一个字符串 s 和一个非空字符串 p,找到 s 中所有是 p 的字母异位词的子串,返回这些子串的起始索引. 字符串只包含小写英文字母,并且字符串 s 和 p 的长度都不超过 201 ...

  8. LeetCode_257. Binary Tree Paths

    257. Binary Tree Paths Easy Given a binary tree, return all root-to-leaf paths. Note: A leaf is a no ...

  9. 深层对象转深层数组(重点:先把对象转数组,直接for in 遍历对象不太可行)

    var json: { PRow0: { style: { width: 10 } }, PTable1: { style: { height: 20 } } } const jsonToArrTre ...

  10. attribute__关键字举例之visibility

    /** @file visibilityT.c * @note * @brief * @author * @date 2019-6-20 * @note v1.0.0 Created * @histo ...