[TJOI2019]唱、跳、rap和篮球——容斥原理+生成函数
先附一组sd图
然后放上原题链接
注意,队伍不同指的是喜好不同,不是人不同
先想到\(DP\),然后你会发现并没有什么优秀的状态设计,然后我们考虑容斥
设\(lim\)表示选的癌坤组数的上限,\(f_i\)为先选出来\(i\)组剩下随便排的方案数,那么答案就是
\[\sum\limits_{i=0}^{lim}(-1)^i\times\ f_i
\]
于是问题转化为了求\(f_i\)。显然\(f_i\)可以表示为一个组合数再乘一个东西,具体来说组合数代表在\(n\)个同学中选\(i\)组癌坤的方案数,后面的那一部分代表满足限制的情况下把\(n-4i\)个人随便排的方案数
先考虑把组合数给搞出来,不难发现\(i\)组癌坤把\(n\)个同学分成了\(i+1\)段,这\(i+1\)段的长度之和为\(n-4i\),由此我们可以得到以下不定方程
\[x_1+x_2+...+x_{i+1}=n-4i
\]
那个组合数就是它的非负整数解个数,这是个经典问题,答案为\(\binom{(n-4i)+(i+1)-1}{(i+1)-1}=\binom{n-3i}{i}\)
而后面那一部分的限制条件看起来用生成函数很好搞,同时还是个排列,考虑把\(4\)个\(EGF\)卷起来,最后后面那一部分的答案即为
\[[x^{n-4i}]\sum\limits_{j=0}^{a-i}\frac{x^j}{j!}\sum\limits_{j=0}^{b-i}\frac{x^j}{j!}\sum\limits_{j=0}^{c-i}\frac{x^j}{j!}\sum\limits_{j=0}^{d-i}\frac{x^j}{j!}
\]
卷积一下就出来了
至此,我们已经得到了一个\(O(n^2logn)\)的大常数做法了,但是好像可以在\(O(n)\)时间内把后面那一部分求出来(可以参考其他题解)?
放上我的超丑代码:
#include <bits/stdc++.h>
using namespace std;
#define N 1000
#define MOD 998244353
int n, cnta, cntb, cntc, cntd, lim, C[N + 5][N + 5], fac[N + 5], facinv[N + 5];
int ans, a[8 * N + 5], b[8 * N + 5], c[8 * N + 5], d[8 * N + 5];
void add(int &x, int y) {
x = (x + y) % MOD;
if (x < 0) x + MOD;
}
int Add(int x, int y) {
return ((x + y) % MOD + MOD) % MOD;
}
int Mul(int x, int y) {
return (1LL * x * y % MOD + MOD) % MOD;
}
int fpow(int x, int p) {
int ret = 1;
while (p) {
if (p & 1) ret = Mul(ret, x);
x = Mul(x, x);
p >>= 1;
}
return ret;
}
void bitReverse(int *s, int len, int bit) {
int swp[8 * N + 5];
for (int i = 1; i < len; ++i) {
swp[i] = (swp[i >> 1] >> 1) | ((i & 1) << (bit - 1));
if (i < swp[i]) swap(s[i], s[swp[i]]);
}
}
void NTT(int *s, int len, int bit, int flag) {
bitReverse(s, len, bit);
for (int l = 2; l <= len; l <<= 1) {
int mid = l >> 1, t = fpow(3, (MOD - 1) / l);
if (flag == -1) t = fpow(t, MOD - 2);
for (int *p = s; p != s + len; p += l) {
int w = 1;
for (int i = 0; i < mid; ++i) {
int x = 1LL * w * p[i + mid] % MOD;
p[i + mid] = (p[i] - x) % MOD;
p[i] = (p[i] + x) % MOD;
w = 1LL * w * t % MOD;
}
}
}
if (flag == -1) {
int invlen = fpow(len, MOD - 2);
for (int i = 0; i < len; ++i)
s[i] = 1LL * s[i] * invlen % MOD;
}
}
int main() {
scanf("%d%d%d%d%d", &n, &cnta, &cntb, &cntc, &cntd);
lim = min(n / 4, min(min(cnta, cntb), min(cntc, cntd)));
for (int i = 0; i <= n; ++i) C[i][0] = 1;
fac[0] = facinv[0] = 1;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= i; ++j)
C[i][j] = Add(C[i - 1][j], C[i - 1][j - 1]);
fac[i] = Mul(fac[i - 1], i);
facinv[i] = fpow(fac[i], MOD - 2);
}
for (int i = 0, l1, l2, l3, l4, len, bit; i <= lim; ++i) {
int conv;
for (int j = 0; j <= 8 * N; ++j) a[j] = b[j] = c[j] = d[j] = 0;
for (int j = 0; j <= cnta - i; ++j) a[j] = facinv[j];
for (int j = 0; j <= cntb - i; ++j) b[j] = facinv[j];
for (int j = 0; j <= cntc - i; ++j) c[j] = facinv[j];
for (int j = 0; j <= cntd - i; ++j) d[j] = facinv[j];
l1 = cnta - i + 1, l2 = cntb - i + 1, l3 = cntc - i + 1, l4 = cntd - i + 1;
bit = 0;
while ((1 << bit) < l1 + l2 - 1) bit++;
len = 1 << bit;
NTT(a, len, bit, 1), NTT(b, len, bit, 1);
for (int j = 0; j < len; ++j) a[j] = Mul(a[j], b[j]);
NTT(a, len, bit, -1);
l1 = l1 + l2 - 1;
bit = 0;
while ((1 << bit) < l3 + l4 - 1) bit++;
len = 1 << bit;
NTT(c, len, bit, 1), NTT(d, len, bit, 1);
for (int j = 0; j < len; ++j) c[j] = Mul(c[j], d[j]);
NTT(c, len, bit, -1);
l2 = l3 + l4 - 1;
bit = 0;
while ((1 << bit) < l1 + l2 - 1) bit++;
len = 1 << bit;
NTT(a, len, bit, 1), NTT(c, len, bit, 1);
for (int j = 0; j < len; ++j) a[j] = Mul(a[j], c[j]);
NTT(a, len, bit, -1);
conv = a[n - 4 * i];
if (i & 1) add(ans, -Mul(C[n - 3 * i][i], Mul(fac[n - 4 * i], conv)));
else add(ans, Mul(C[n - 3 * i][i], Mul(fac[n - 4 * i], conv)));
}
while (ans < 0) ans += MOD;
printf("%d\n", ans);
return 0;
}