首页 > 其他分享 >CodeForces 995F Cowmpany Cowmpensation

CodeForces 995F Cowmpany Cowmpensation

时间:2024-01-26 21:59:24浏览次数:29  
标签:typedef Cowmpany limits res ll 995F long Cowmpensation define

洛谷传送门

CF 传送门

考虑一个显然的树形 dp,设 \(f_{u, i}\) 为 \(u\) 结点染颜色 \(i\) 的方案数,有 \(f_{u, i} = \prod\limits_{v \in son_u} \sum\limits_{j = 1}^i f_{v, j}\)。前缀和后可得 \(f_{u, i} = f_{u, i - 1} + \prod\limits_{v \in son_u} f_{v, i}\)。

发现 \(f_u(x)\) 为最高次数为 \(sz_u\) 的多项式。考虑归纳证明,叶子结点有 \(f_u(x) = x\)。差分后是儿子的多项式乘积,次数为 \(\sum\limits_{v \in son_u} sz_v = sz_u - 1\)。前缀和后有次数为 \(sz_u\)。

于是树形 dp 算出 \(d = 1, 2, \ldots, n\) 的答案,拉格朗日插值即可。

时间复杂度 \(O(n^2)\)。

code
// Problem: F. Cowmpany Cowmpensation
// Contest: Codeforces - Codeforces Round 492 (Div. 1) [Thanks, uDebug!]
// URL: https://codeforces.com/problemset/problem/995/F
// Memory Limit: 256 MB
// Time Limit: 2000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include <bits/stdc++.h>
#define pb emplace_back
#define fst first
#define scd second
#define mkp make_pair
#define mems(a, x) memset((a), (x), sizeof(a))

using namespace std;
typedef long long ll;
typedef double db;
typedef unsigned long long ull;
typedef long double ldb;
typedef pair<ll, ll> pii;

const int maxn = 3030;
const ll mod = 1000000007;

inline ll qpow(ll b, ll p) {
	ll res = 1;
	while (p) {
		if (p & 1) {
			res = res * b % mod;
		}
		b = b * b % mod;
		p >>= 1;
	}
	return res;
}

ll n, m, f[maxn][maxn];
vector<int> G[maxn];

void dfs(int u) {
	for (int v : G[u]) {
		dfs(v);
	}
	for (int i = 1; i <= n; ++i) {
		f[u][i] = f[u][i - 1];
		ll mul = 1;
		for (int v : G[u]) {
			mul = mul * f[v][i] % mod;
		}
		f[u][i] = (f[u][i] + mul) % mod;
	}
}

void solve() {
	scanf("%lld%lld", &n, &m);
	for (int i = 2, p; i <= n; ++i) {
		scanf("%d", &p);
		G[p].pb(i);
	}
	dfs(1);
	ll ans = 0;
	for (int i = 0; i <= n; ++i) {
		ll x = 1, y = 1;
		for (int j = 0; j <= n; ++j) {
			if (i == j) {
				continue;
			}
			x = x * (m - j + mod) % mod;
			y = y * (i - j + mod) % mod;
		}
		ans = (ans + f[1][i] * x % mod * qpow(y, mod - 2)) % mod;
	}
	printf("%lld\n", ans);
}

int main() {
	int T = 1;
	// scanf("%d", &T);
	while (T--) {
		solve();
	}
	return 0;
}

标签:typedef,Cowmpany,limits,res,ll,995F,long,Cowmpensation,define
From: https://www.cnblogs.com/zltzlt-blog/p/17990790

相关文章

  • 【CF995F Cowmpany Cowmpensation】(dp+容斥)
    原题链接题意一棵\(n\)个节点的树,给每个节点分配工资(\([1,D]\)),子节点不能超过父亲节点的工资,问有多少种分配方案。$1\len\le3000$,$1\leD\le10^9$思......
  • CF995F Cowmpany Cowmpensation
    链接:https://www.luogu.com.cn/problem/CF995F题目描述:树形结构,给每个节点分配工资[\(1\),\(d\)],子节点不能超过父亲节点的工资,问有多少种分配方案。题解:首先我们可以......