Blog of RuSun

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

UOJ450. 【集训队作业2018】复读机

UOJ450. 【集训队作业2018】复读机

考虑每个颜色选择几个,那么有 EGF $F(x) = \sum _ {d | i} \frac {x ^ i} {i!}$ ,最终答案为 $n![x ^ n] F(x) ^ k$ ,但是这并不好化作封闭形式。考虑单位根反演。

$$
\begin {aligned}
F(x) & = \sum _ {d | i} \frac {x ^ i} {i!} \\
& = \sum _ {i = 0} \frac {x ^ i} {i!} [d | i] \\
& = \sum _ {i = 0} \frac {x ^ i} {i!} (\frac 1 d \sum _ {k = 0} ^ {d - 1} \omega _ d ^ {ik}) \\
& = \frac 1 d \sum _ {k = 0} ^ {d - 1} \frac {(x\omega _ d ^ k) ^ i}{i!} \\
& = \frac 1 d \sum _ {k = 0} ^ {d - 1} e ^ {x\omega _ d ^ k}\\
\end {aligned}
$$

$$
\begin {aligned}
ANS & = n![x ^ n] F(x) ^ k\\
& = n![x ^ n] (\frac 1 d \sum _ {i = 0} ^ {d - 1} e ^ {x\omega _ d ^ i}) ^ k \\
& = \frac {n!} {d ^ k} [x ^ n] (\sum _ {k = 0} ^ {d - 1} e ^ {x\omega _ d ^ i}) ^ k
\end {aligned}
$$

对于 $d = 1$ ,$ANS = n! [x ^ n] e ^ {xk} = n![x ^ n]\sum _ {i = 0} \frac {(xk) ^ i} {i!} = k ^ n$ 。

对于 $d = 2$ ,$ANS = \frac {n!}{2 ^ k} [x ^ n](e ^ x + e ^ {\omega _ 2 x}) ^ k = \frac {n!}{2 ^ k} [x ^ n]\sum _ {i = 0} ^ k \binom k i e ^ {ix + (k - i)(-x)} = \frac 1 {2 ^ k} \sum _ {i = 0} ^ k \binom k i (2i - k) ^ n$ 。

对于 $d = 3$ ,$ANS = \frac 1 {3 ^ k} \sum _ {i = 0} ^ k \sum _ {j = 0} ^ i \binom k i \binom i j (m - i + j \omega _ 3 + (i - j) \omega _ 3 ^ 2)$。

查看代码
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
#include <cstdio>
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('0' + x % 10);
}
typedef long long LL;
const int N = 5e5 + 10, mod = 19491001;
int n, m, d, fact[N], ifact[N];
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 init ()
{
fact[0] = ifact[0] = 1;
for (int i = 1; i < N; ++i)
fact[i] = (LL)fact[i - 1] * i % mod;
ifact[N - 1] = binpow(fact[N - 1]);
for (int i = N - 1; i; --i)
ifact[i - 1] = (LL)ifact[i] * i % mod;
}
int C (int a, int b) { return a < b ? 0 : (LL)fact[a] * ifact[b] % mod * ifact[a - b] % mod; }
int main ()
{
init();
read(n, m, d);
if (d == 1) return write(binpow(m, n)), 0;
if (d == 2)
{
int res = 0;
for (int i = 0; i <= m; ++i)
res = (res + (LL)C(m, i) * binpow(i + i - m, n)) % mod;
write(((LL)res * binpow(2, -m + mod - 1) % mod + mod) % mod);
}
else if (d == 3)
{
int w = binpow(7, (mod - 1) / 3), res = 0;
for (int i = 0; i <= m; ++i)
for (int j = 0; j <= i; ++j)
res = (res + (LL)C(m, i) * C(i, j) % mod * binpow(m - i + (j + (LL)w * (i - j) % mod) * w % mod, n)) % mod;
write(((LL)res * binpow(3, -m + mod - 1) % mod + mod) % mod);
}
return 0;
}