洛谷 P5644 [PKUWC2018] 猎人杀

洛谷传送门

感觉跟 CF Gym 102978H Harsh Comments 很像。

考虑容斥,钦定 \(S \subseteq [2, n]\) 中的人比 \(1\) 后死。设 \(P(S)\)\(S\) 中的人比 \(1\) 后死的概率,那么答案为:

\[ans = \sum\limits_{S \subseteq [2, n]} (-1)^{|S|} P(S) \]

至于 \(P(S)\) 的计算,考虑这样一个问题:现在的全集是 \(\{1\} \cup S\),我们要从全集中选一个人作为第一个死的,每个人被选中的概率与 \(w_i\) 成正比,那么 \(1\) 被选中的概率为 \(\frac{w_1}{w_1 + \sum\limits_{i \in S} w_i}\)

因此:

\[ans = \sum\limits_{S \subseteq [2, n]} (-1)^{|S|} \frac{w_1}{w_1 + \sum\limits_{i \in S} w_i} \]

发现 \(\sum w_i\) 很小,考虑对 \(i \in [2, n]\)\(w_i\) 做一个 0/1 背包,可以求出 \(f_i = \sum\limits_{S \in [2, n]} (-1)^{|S|} [\sum\limits_{j \in S} w_j = i]\),给每个物品赋一个 \(-1\) 的权即可。直接做是 \(O(n \sum w_i)\) 的,可以拿到 \(80\) 分。

这种背包的题一般考虑构造多项式然后分治 NTT 求解。考虑构造 \(F(x) = \prod\limits_{i = 2}^n (1 - x^{w_i})\),那么 \(f_i = [x^i] F(x)\)。分治 NTT 求出 \(F(x)\) 即可。时间复杂度 \(O(\sum w_i \log^2 \sum w_i)\)

code
// Problem: P5644 [PKUWC2018] 猎人杀
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P5644
// Memory Limit: 500 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

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

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

const int maxn = 500100;
const ll mod = 998244353, G = 3;

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, a[maxn], r[maxn];

typedef vector<ll> poly;

inline poly NTT(poly a, int op) {
	int n = (int)a.size();
	for (int i = 0; i < n; ++i) {
		if (i < r[i]) {
			swap(a[i], a[r[i]]);
		}
	}
	for (int k = 1; k < n; k <<= 1) {
		ll wn = qpow(op == 1 ? G : qpow(G, mod - 2), (mod - 1) / (k << 1));
		for (int i = 0; i < n; i += (k << 1)) {
			ll w = 1;
			for (int j = 0; j < k; ++j, w = w * wn % mod) {
				ll x = a[i + j], y = w * a[i + j + k] % mod;
				a[i + j] = (x + y) % mod;
				a[i + j + k] = (x - y + mod) % mod;
			}
		}
	}
	if (op == -1) {
		ll inv = qpow(n, mod - 2);
		for (int i = 0; i < n; ++i) {
			a[i] = a[i] * inv % mod;
		}
	}
	return a;
}

inline poly operator * (poly a, poly b) {
	a = NTT(a, 1);
	b = NTT(b, 1);
	int n = (int)a.size();
	for (int i = 0; i < n; ++i) {
		a[i] = a[i] * b[i] % mod;
	}
	a = NTT(a, -1);
	return a;
}

inline poly mul(poly a, poly b) {
	int n = (int)a.size() - 1, m = (int)b.size() - 1, k = 0;
	while ((1 << k) <= n + m + 1) {
		++k;
	}
	for (int i = 1; i < (1 << k); ++i) {
		r[i] = (r[i >> 1] >> 1) | ((i & 1) << (k - 1));
	}
	poly A(1 << k), B(1 << k);
	for (int i = 0; i <= n; ++i) {
		A[i] = a[i];
	}
	for (int i = 0; i <= m; ++i) {
		B[i] = b[i];
	}
	poly res = A * B;
	res.resize(n + m + 1);
	return res;
}

poly dfs(int l, int r) {
	if (l == r) {
		poly A(a[l] + 1);
		A[0] = 1;
		A[a[l]] = mod - 1;
		return A;
	}
	int mid = (l + r) >> 1;
	poly A = dfs(l, mid), B = dfs(mid + 1, r);
	return mul(A, B);
}

void solve() {
	scanf("%lld", &n);
	for (int i = 1; i <= n; ++i) {
		scanf("%lld", &a[i]);
		if (i > 1) {
			m += a[i];
		}
	}
	poly f = dfs(2, n);
	ll ans = 0;
	for (int i = 0; i <= m; ++i) {
		ans = (ans + f[i] * a[1] % mod * qpow(a[1] + i, mod - 2) % mod) % mod;
	}
	printf("%lld\n", ans);
}

int main() {
	int T = 1;
	// scanf("%d", &T);
	while (T--) {
		solve();
	}
	return 0;
}
posted @ 2023-07-18 16:51  zltzlt  阅读(25)  评论(0编辑  收藏  举报