CF 954H Path Counting
You are given a rooted tree. Let's denote d(x) as depth of node x: depth of the root is 1, depth of any other node x is d(y) + 1, where yis a parent of x.
The tree has the following property: every node x with d(x) = i has exactly ai children. Maximum possible depth of a node is n, and an = 0.
We define fk as the number of unordered pairs of vertices in the tree such that the number of edges on the simple path between them is equal to k.
Calculate fk modulo 109 + 7 for every 1 ≤ k ≤ 2n - 2.
The first line of input contains an integer n (2 ≤ n ≤ 5 000) — the maximum depth of a node.
The second line of input contains n - 1 integers a1, a2, ..., an - 1 (2 ≤ ai ≤ 109), where ai is the number of children of every node xsuch that d(x) = i. Since an = 0, it is not given in the input.
Print 2n - 2 numbers. The k-th of these numbers must be equal to fk modulo 109 + 7.
【题意】
给出一棵深度为n的树,其中每个深度为i的节点都有a[i]个儿子。问对于每个k,有多少条简单路径满足其长度恰好为k。
n<=5000
【分析】
考虑枚举路径的端点。
设d[i,j]表示从某个深度为i的节点开始,只往下走且长度为j的路径条数。那么d[i,j]显然等于i的子树中深度为i+j的点数。
设u[i,j]表示从某个深度为i的节点开始,第一步必须往上走,且路径长度为j的方案。
有两种转移,一种是走到父亲后继续往上,贡献就等于u[i-1,j-1]。另一种转移是走到父亲后就开始往下走,贡献就等于u[i,j-2]*(a[i-1]-1)
综上,f[i][j]= f[i+1][j-1]*a[i]+
f[i-1][j-1]+
f[i][j-2]*(a[i-1]-1);
【代码】
__EOF__

本文链接:https://www.cnblogs.com/shenben/p/10424160.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角【推荐】一下。您的鼓励是博主的最大动力!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
2017-02-23 2120: 数颜色
2017-02-23 2821: 作诗(Poetize)
2017-02-23 分块常见例题
2017-02-23 P3373 【模板】线段树 2