CodeForces 1575F Finding Expected Value

洛谷传送门

CF 传送门

考虑单个序列如何求答案。

考虑鞅与停时定理。定义一个局面的势能为 \(\sum\limits_{i = 0}^{K - 1} f(b_i)\),其中 \(f(x)\) 是一个关于 \(x\) 的函数,\(b_i\)\(i\) 的出现次数。那么我们要构造 \(f(x)\),使得每次操作,局面势能期望减少 \(1\),那么期望步数就可以用初始局面的势能 \(\sum\limits_{i = 0}^{K - 1} f(b_i)\) 减去终止局面的势能 \(f(n)\) 计算出。

要求局面势能期望减少 \(1\),也就是:

\[\sum\limits_{i = 1}^K \frac{a_i}{n} \sum\limits_{j = 1}^K [i \ne j] \frac{1}{K} (f(a_i) - f(a_i - 1) + f(a_j) - f(a_j + 1)) = 1 \]

\[\sum\limits_{i = 1}^K \sum\limits_{j = 1}^K [i \ne j] a_i (f(a_i) - f(a_i - 1) + f(a_j) - f(a_j + 1)) = nK \]

\[\sum\limits_{i = 1}^K (K - 1) a_i (f(a_i) - f(a_i - 1)) + (n - a_i) (f(a_i) - f(a_i + 1)) = nK \]

不妨让对于每个 \(i\),求和号后面的式子都等于 \(n\)。也就是对于每个非负整数 \(x\),都满足:

\[(K - 1) x (f(x) - f(x - 1)) + (n - x) (f(x) - f(x + 1)) = n \]

移项得:

\[\frac{(K - 1) x (f(x) - f(x - 1)) + (n - x) f(x) - n}{n - x} = f(x + 1) \]

这里我们钦定 \(f(0) = 0\)。枚举 \(x\) 即可递推 \(f(x)\)

再来考虑怎么计数。把 \(-1\) 替换成 \([0, K - 1]\) 的数后,有些数的出现次数会改变。不妨枚举一种原来出现次数为 \(j\) 的数替换完 \(-1\) 后出现次数变为 \(i\),设原来有 \(c_k\) 个出现次数为 \(k\) 的数,有 \(c\)\(-1\),可得答案为:

\[\sum\limits_{i = 1}^n f(i) \sum\limits_{j = 0}^i c_j \binom{c}{i - j} (K - 1)^{c - (i - j)} \]

可以直接 MTT 做到 \(O(n \log n)\),但是注意到 \(c_j\) 有值的 \(j\) 的个数是 \(O(\sqrt n)\) 的,直接暴力枚举有值的位置计算就是 \(O(n \sqrt n)\)

code
// Problem: F. Finding Expected Value
// Contest: Codeforces - COMPFEST 13 - Finals Online Mirror (Unrated, ICPC Rules, Teams Preferred)
// URL: https://codeforces.com/problemset/problem/1575/F
// Memory Limit: 512 MB
// Time Limit: 3000 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 __int128 lll;
typedef double db;
typedef unsigned long long ull;
typedef long double ldb;
typedef pair<ll, ll> pii;

const int maxn = 600100;
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, a[maxn], f[maxn], m, b[maxn], inv[maxn], fac[maxn], ifac[maxn], pw[maxn];

inline ll C(ll n, ll m) {
	if (n < m || n < 0 || m < 0) {
		return 0;
	} else {
		return fac[n] * ifac[m] % mod * ifac[n - m] % mod;
	}
}

void solve() {
	scanf("%lld%lld", &n, &m);
	pw[0] = 1;
	for (int i = 1; i <= n; ++i) {
		pw[i] = pw[i - 1] * (m - 1) % mod;
	}
	inv[1] = 1;
	for (int i = 2; i <= n; ++i) {
		inv[i] = (mod - mod / i) * inv[mod % i] % mod;
	}
	fac[0] = ifac[0] = 1;
	for (int i = 1; i <= n; ++i) {
		fac[i] = fac[i - 1] * i % mod;
		ifac[i] = ifac[i - 1] * inv[i] % mod;
	}
	map<ll, ll> mp;
	ll cnt = 0;
	for (int i = 1; i <= n; ++i) {
		scanf("%lld", &a[i]);
		if (a[i] != -1) {
			++mp[a[i]];
		} else {
			++cnt;
		}
	}
	for (int i = 0; i < n; ++i) {
		f[i + 1] = ((m - 1) * i % mod * (f[i] - f[i - 1] + mod) % mod + (n - i) * f[i] - n + mod) % mod * inv[n - i] % mod;
	}
	b[0] = m - (ll)mp.size();
	for (pii p : mp) {
		++b[p.scd];
	}
	vector<int> vc;
	for (int j = 0; j <= n; ++j) {
		if (b[j]) {
			vc.pb(j);
		}
	}
	ll ans = 0;
	for (int i = 1; i <= n; ++i) {
		ll res = 0;
		for (int j : vc) {
			if (i - j > cnt) {
				continue;
			}
			if (i < j) {
				break;
			}
			res = (res + b[j] * C(cnt, i - j) % mod * pw[cnt - (i - j)]) % mod;
		}
		ans = (ans + res * f[i]) % mod;
	}
	ans = (ans * qpow(qpow(m, cnt), mod - 2) - f[n] + mod) % mod;
	printf("%lld\n", ans);
}

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

posted @ 2024-08-15 23:23  zltzlt  阅读(6)  评论(0编辑  收藏  举报