ABC331G 题解

· · 题解

刷点计数防止小脑萎缩。

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

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

\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}

求个和:

\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}

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

\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(n\log^2 n)。

AC 代码

#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);
}