跳到正文

题解

ABC331G 题解

Register_int约 4 分钟
原文
文章信息与版本

文章信息

发布
2023年12月2日
收录
2026年10月14日
更新
2026年11月21日
篇幅
约 4 分钟
获赞
7

版本

  1. v1当前

刷点计数防止小脑萎缩。

枚举我们抽卡的张数。首先有熟知结论 ∑i×P(X=i)=∑P(X≤i)\sum i\times P(X=i)=\sum P(X\le i),所以我们可以先求出抽 kk 张卡后每种卡都至少有一张的概率,再加和即可。

然而并不好处理,我们可以先求出至少缺一种卡片的概率。考虑容斥,设 SS 为所有数字卡片的集合,TT 为我们当前取的卡片所属于的集合,则有:

P(X≤k)=1−∑T⊆S(−1)m−∣T∣(∑i∈Tcin)k=∑T⊊S(−1)m−∣T∣−1(∑i∈Tcin)k=(−1)m−1∑T⊊S(−1)∣T∣(∑i∈Tcin)k\begin{aligned} P(X\le k)&=1-\sum_{T\subseteq S}(-1)^{m-|T|}\left(\dfrac{\sum_{i\in T}c_i}n\right)^k\\ &=\sum_{T\subsetneq S}(-1)^{m-|T|-1}\left(\dfrac{\sum_{i\in T}c_i}n\right)^k\\ &=(-1)^{m-1}\sum_{T\subsetneq S}(-1)^{|T|}\left(\dfrac{\sum_{i\in T}c_i}n\right)^k\\ \end{aligned}

求个和:

∑kP(X≤k)=(−1)m−1∑k∑T⊊S(−1)∣T∣(∑i∈Tcin)k=(−1)m−1∑T⊊S(−1)∣T∣∑k(∑i∈Tcin)k=(−1)m−1∑T⊊S(−1)∣T∣nn−∑i∈Tci\begin{aligned} \sum_k P(X\le k)&=(-1)^{m-1}\sum_k\sum_{T\subsetneq S}(-1)^{|T|}\left(\dfrac{\sum_{i\in T}c_i}n\right)^k\\ &=(-1)^{m-1}\sum_{T\subsetneq S}(-1)^{|T|}\sum_k\left(\dfrac{\sum_{i\in T}c_i}n\right)^k\\ &=(-1)^{m-1}\sum_{T\subsetneq S}(-1)^{|T|}\dfrac n{n-\sum_{i\in T}c_i} \end{aligned}

感觉很难处理,怎么办呢?考虑枚举 ∑i∈Tci\sum_{i\in T}c_i,那么这一部分其实就是经典的背包方案数。容斥系数可以塞进相乘的过程中,所以:

(−1)m−1∑T⊊S(−1)∣T∣nn−∑i∈Tci=(−1)m−1∑kn−1nn−k[xk]∏i(1−xci)\begin{aligned} &(-1)^{m-1}\sum_{T\subsetneq S}(-1)^{|T|}\dfrac n{n-\sum_{i\in T}c_i}\\ &=(-1)^{m-1}\sum^{n-1}_k\dfrac n{n-k}[x^k]\prod_i(1-x^{c_i}) \end{aligned}

压力给到多项式相乘。我们可以贪心,每次选择两个最短的多项式进行合并,时间复杂度为 O(nlog⁡2n)O(n\log^2 n)。

AC 代码

C++
#include <bits/stdc++.h>

using namespace std;

#define _POW_OPTIMIZE_FOR_K

namespace polynomial {
	// by Register_int.
}

using namespace polynomial;

const int MAXN = 2e5 + 10;

struct node {
	int len, id;
	bool operator < (const node &rhs) const { return len > rhs.len; }
}; priority_queue<node> q;

int n, m, k; poly<ll> a[MAXN]; ll ans;

int main() {
	scanf("%d%d", &n, &m);
	for (int i = 1, x; i <= m; i++) {
		scanf("%d", &x), a[i].resize(x + 1);
		a[i][0] = 1, a[i][x] = mod - 1, q.push({ x + 1, i });
	}
	for (int x, y, l; q.size() > 1;) {
		l = -1;
		x = q.top().id, l += q.top().len, q.pop();
		y = q.top().id, l += q.top().len, q.pop();
		a[x].resize(l), a[y].resize(l), a[x] *= a[y], q.push({ l, x });
	}
	k = q.top().id;
	for (int i = 0; i < a[k].size(); i++) ans = (ans + n * inv(n - i) % mod * a[k][i]) % mod;
	printf("%lld", m & 1 ? ans : mod - ans);
}
原文
存档评论1 条抓取于