CodeForces-H Path Counting(DP)

H. Path Counting

time limit per test

5 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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 y is 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.

Input

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 x such that d(x) = i. Since an = 0, it is not given in the input.

Output

Print 2n - 2 numbers. The k-th of these numbers must be equal to fk modulo 109 + 7.

Examples

input

Copy

4
2 2 2

output

Copy

14 19 20 20 16 16 

input

Copy

3
2 3

output

Copy

8 13 6 9 

Note

This the tree from the first sample:

题意:一棵深度为n的树,深度为i的节点有a[i]个儿子,问树上有多少条长度为x的链,x∈[1,k]

题解:首先很容易想到用DP做

设f[i][j]为以深度为i的点为端点,往下走j步的方案数,pre[i]表示深度为i的节点的个数,dp[i][j]表示以i为LCA长度为j的链的个数

f[i][j] = a[i] * f[i + 1][j - 1],f[n][0]=1

pre[i]=\prod_{j=1}^{i}a[i]

然后很容易想到一个DP方程:dp[i][j]=pre[i - 1]*C^{2}_{a[i]}*\sum_{k=0}^{j}f[i+1][k]*f[i+1][j-k]

右边用可取模的FFT转移一下就行了。。。原本是这么想的。。。然而O(n^2logn)过不了。。。

转换一下思路:设dp[i][j][k]为以i为端点,向上走k步,长度为j的链的个数

可以得到:dp[i][j][k] = dp[i-1][j-1][k-1],dp[i][j][2]=f[i][j-2]*(a[i-1]-1)

发现第三维可以用前缀和优化,得到dp[i][j]:以i为端点,向上至少走一步,长度为j的链的个数

dp[i][j]=dp[i-1][j-1]+f[i][j-2]*(a[i-1]-1)

时间复杂度为O(n^2)

然后这道题很苟地卡空间,所以dp数组和f数组需要重用一下,因此j需要从大到小枚举

#include<bits/stdc++.h>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define x first
#define y second
#define rep(i,a,b) for(int i=a;i<(b);++i)
#define per(i,a,b) for(int i=a-1;i>=(b);--i)
#define fuck(x) cout<<'['<<#x<<' '<<(x)<<']'
#define clr(a,b) memset(a,b,sizeof(a))
#define eps 1e-10
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> VI;
typedef pair<int, int> pii;
typedef unsigned int ui;
const int INF = 0x3f3f3f3f;
const ll INFLL = 0x3f3f3f3f3f3f3f3fLL;
const int mod = 1e9 + 7;
const int N = 5e3 + 5;

int inv2 = (mod + 1) / 2;
int a[N], f[N][N << 1], pre[N], ans[N << 1];
ll mul(ll x, ll y) {return x * y % mod;}

int main() {
#ifdef local
    freopen("in.txt", "r", stdin);
#endif // local

    int n; cin >> n;
    rep(i, 1, n) cin >> a[i];
    pre[0] = 1; rep(i, 1, n) pre[i] = (ll)pre[i - 1] * a[i] % mod;
    rep(i, 1, n + 1) f[i][0] = 1;
    per(i, n, 1) rep(j, 1, n - i + 1) {
        f[i][j] = (ll)a[i] * f[i + 1][j - 1] % mod;
        ans[j] = (ans[j] + (ll) pre[i - 1] * f[i][j]) % mod;
    }
    rep(i, 1, n + 1) per(j, 2 * n - 1, 1) {
        f[i][j] = f[i - 1][j - 1];
        if(j > 1 && i > 1) f[i][j] = (f[i][j] + mul(f[i][j - 2], a[i - 1] - 1)) % mod;
        ans[j] = (ans[j] + (ll) f[i][j] * pre[i - 1]) % mod;
    }
    rep(i, 1, 2 * n - 1) printf("%lld%c", (ll)ans[i] * inv2 % mod, i == 2 * n - 2 ? '\n' : ' ');
    return 0;
}

 

引用\[1\]中提到了一种树形动态规划的方法来解决CodeForces - 982C问题。在这个问题中,subtree指的是子连通块,而不是子树。为了使cnt_white - cnt_black尽可能大,可以使用两次树形动态规划来求解。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子树中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的树链所代表的子树。在第二次遍历中,需要维护一个sum变量,用于存储树链所代表的子树的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。如果ans\[u\]为正,则减去dp\[v\]就是树链所代表的子树的权值。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] 问题: CodeForces - 982C 树形DP是什么问题?如何解决? 回答: CodeForces - 982C是一个树形动态规划问题。在这个问题中,需要求解子连通块的最大权值和,使得cnt_white - cnt_black尽可能大。解决这个问题的方法是使用两次树形动态规划。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子树中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的树链所代表的子树。在第二次遍历中,需要维护一个sum变量,用于存储树链所代表的子树的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] #### 引用[.reference_title] - *1* *2* [CodeForces - 1324F Maximum White Subtree(树形dp)](https://blog.csdn.net/qq_45458915/article/details/104831678)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值