Educational Codeforces Round 40 H. Path Counting DP

版权声明:xgc原创文章,未经允许不得转载。 https://blog.csdn.net/xgc_woker/article/details/82935498

Description
给你一棵树,第i层每一个节点都有a[i-1]个子节点。
让你求对于每一个k,长度为k的有序点对个数。


Sample Input
4
2 2 2


Sample Output
14 19 20 20 16 16


设f[i][j][k]为到第i层,长度为j的有序点对。
k=0时表示有一个端点处于这层,k=1时表示有两个端点处于这层的方案数。
直接转移即可。


#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;
typedef long long LL;
const LL mod = 1e9 + 7;
int _min(int x, int y) {return x < y ? x : y;}
int _max(int x, int y) {return x > y ? x : y;}
int read() {
	int s = 0, f = 1; char ch = getchar();
	while(ch < '0' || ch > '9') {if(ch == '-') f = -1; ch = getchar();}
	while(ch >= '0' && ch <= '9') s = s * 10 + ch - '0', ch = getchar();
	return s * f;
}

LL f[2][2][10010], ans[10010], a[5100];

int main() {
	int n = read();
	for(int i = 2; i <= n; i++) a[i] = read();
	f[0][0][0] = 1;
	int now = 0;
	for(int i = 2; i <= n; i++) {
		now ^= 1; memset(f[now], 0, sizeof(f[now]));
		f[now][0][0] = f[now ^ 1][0][0] * a[i] % mod;
		f[now][0][1] = f[now][0][0];
		f[now][1][2] = f[1 ^ now][0][0] * ((a[i] * (a[i] - 1) / 2) % mod) % mod;
		for(int j = 4; j <= 2 * i - 2; j++) f[now][1][j] = (f[1 ^ now][1][j - 2] * a[i] % mod) * a[i] % mod;
		for(int j = 2; j < 2 * i - 2; j++) {
			f[now][0][j] = f[1 ^ now][0][j - 1] * a[i] % mod;
			if(j % 2 == 1) (f[now][0][j] += (f[1 ^ now][1][j - 1] * 2LL % mod) * a[i] % mod) %= mod;
		}
		for(int j = 1; j <= 2 * i - 2; j++) {
			(ans[j] += f[now][0][j]) %= mod;
			if(j % 2 == 0) (ans[j] += f[now][1][j]) %= mod;
		}
	} for(int i = 1; i <= 2 * n - 2; i++) printf("%lld ", ans[i]);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/xgc_woker/article/details/82935498