ABC331G 题解
Register_int · · 题解
刷点计数防止小脑萎缩。
枚举我们抽卡的张数。首先有熟知结论
然而并不好处理,我们可以先求出至少缺一种卡片的概率。考虑容斥,设
求个和:
感觉很难处理,怎么办呢?考虑枚举
压力给到多项式相乘。我们可以贪心,每次选择两个最短的多项式进行合并,时间复杂度为
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);
}