Blog of RuSun

\begin {array}{c} \mathfrak {One Problem Is Difficult} \\\\ \mathfrak {Because You Don't Know} \\\\ \mathfrak {Why It Is Diffucult} \end {array}

P5339 [TJOI2019]唱、跳、rap和篮球

P5339 [TJOI2019]唱、跳、rap和篮球

钦定 $k$ 个位置,剩下的随意选择,那么有 $F(k) = \binom {n - 3k} k S(a - k, b - k, c - k, d - k, n - 4k)$ ,其中 $S(a, b, c, d, n)$ 表示 $n$ 个位置随便选择,并且不超过限制 $a, b, c, d$ 。当 $a + b + c + d = n$ 时,是经典的可重排列,答案为 $\frac {n!} {a!b!c!d!}$ ,当 $a + b + c + d > n$ 时,考虑暴力枚举 $a, b, c, d$, 那么有 $\sum _ {i = 0} ^ a \sum _ {j = 0} ^ b \sum _ {k = 0} ^ c \sum _ {l = 0} ^ d = [i + j + k + l = n] \frac {(i + j + k + l)!} {i!j!k!l!}$ 。因为只要 $i + j + k + l = n$ 的贡献,那么答案可以写做:$\sum _ {i = 0} ^ a \sum _ {j = 0} ^ b \sum _ {k = 0} ^ c \sum _ {l = 0} ^ d = [i + j + k + l = n] \frac {n!} {i!j!k!l!}$ 。虽然我们只需要卷积中的一项,但是因为有四个式子,所以不能直接做完,用 ntt 即可。

$F(x) = \sum _ {i = x} \binom i x G(i)$ ,那么有 $G(x) = \sum _ {i = x} \binom i x (-1) ^ {i - x} F(i)$ ,答案为 $G(0)$ 。

查看代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include <cstdio>
#include <algorithm>
using namespace std;
template <class Type>
void read (Type &x)
{
char c;
bool flag = false;
while ((c = getchar()) < '0' || c > '9')
flag |= c == '-';
x = c - '0';
while ((c = getchar()) >= '0' && c <= '9')
x = (x << 3) + (x << 1) + c - '0';
if (flag) x = ~x + 1;
}
template <class Type, class ...Rest>
void read (Type &x, Rest &...y) { read(x); read(y...); }
template <class Type>
void write (Type x)
{
if (x < 0) putchar('-'), x = ~x + 1;
if (x > 9) write(x / 10);
putchar(char('0' + x % 10));
}
typedef long long LL;
const int N = 5e3 + 10, mod = 998244353;
int rev[N];
int inv[N], fact[N], ifact[N], p2[N];
void init ()
{
inv[1] = 1;
for (int i = 2; i < N; ++i)
inv[i] = -(LL)(mod / i) * inv[mod % i] % mod;
fact[0] = ifact[0] = 1;
for (int i = 1; i < N; ++i)
{
fact[i] = (LL)fact[i - 1] * i % mod;
ifact[i] = (LL)ifact[i - 1] * inv[i] % mod;
}
p2[0] = 1;
for (int i = 1; i < N; ++i)
p2[i] = p2[i - 1] * 2ll % mod;
}
int C (int a, int b) { return a < b ? 0 : (LL)fact[a] * ifact[b] % mod * ifact[a - b] % mod; }
int binpow (int b, int k = mod - 2)
{
int res = 1;
for (; k; k >>= 1, b = (LL)b * b % mod)
if (k & 1) res = (LL)res * b % mod;
return res;
}
void ntt (int *x, int bit, int op)
{
int tot = 1 << bit;
for (int i = 0; i < tot; ++i)
if ((rev[i] = rev[i >> 1] >> 1 | (i & 1) << bit - 1) > i)
swap(x[i], x[rev[i]]);
for (int mid = 1; mid < tot; mid <<= 1)
{
int w1 = binpow(3, (mod - 1) / (mid << 1));
if (!~op) w1 = binpow(w1);
for (int i = 0; i < tot; i += mid << 1)
for (int j = 0, k = 1; j < mid; ++j, k = (LL)k * w1 % mod)
{
int p = x[i + j], q = (LL)k * x[i + j + mid] % mod;
x[i + j] = (p + q) % mod, x[i + j + mid] = (p - q) % mod;
}
}
if (~op) return;
int itot = binpow(tot);
for (int i = 0; i < tot; ++i)
x[i] = (LL)x[i] * itot % mod;
}
int calc (int a, int b, int c, int d, int n)
{
static int A[N], B[N], C[N], D[N];
a = min(a, n), b = min(b, n), c = min(c, n), d = min(d, n);
int bit = 0;
while (1 << bit < a + b + c + d + 1) ++bit;
int tot = 1 << bit;
for (int i = 0; i < tot; ++i) A[i] = (i <= a) * ifact[i];
for (int i = 0; i < tot; ++i) B[i] = (i <= b) * ifact[i];
for (int i = 0; i < tot; ++i) C[i] = (i <= c) * ifact[i];
for (int i = 0; i < tot; ++i) D[i] = (i <= d) * ifact[i];
ntt(A, bit, 1), ntt(B, bit, 1), ntt(C, bit, 1), ntt(D, bit, 1);
for (int i = 0; i < tot; ++i)
A[i] = (LL)A[i] * B[i] % mod * C[i] % mod * D[i] % mod;
ntt(A, bit, -1);
return (LL)A[n] * fact[n] % mod;
}
int main ()
{
init();
int n, a, b, c, d;
read(n, a, b, c, d);
int res = 0;
for (int i = 0; i * 4 <= n; ++i)
res = (res + (i & 1 ? -1ll : 1ll) * C(n - 3 * i, i) * calc(a - i, b - i, c - i, d - i, n - i * 4)) % mod;
write((res + mod) % mod);
return 0;
}