Blog of RuSun

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

LOJ6538. 烷基计数 加强版 加强版

LOJ6538. 烷基计数 加强版 加强版

对于每个节点,最多有 $3$ 个儿子。考虑生成函数,不能简单认为答案为 $F(x) = 1 + x F(x) ^ 3$ ,因为子树可能是相同的,这样就会重复计算。令 $A$ 表示有两个子树相同的方案数,$B$ 表示有三个子树相同的方案数,那么有 $[x ^ {2n}]A(x) = [x ^ {3n}]B(x) = [x ^ n]F(x)$ 。考虑 Burnside引理 ,子树完全相同才能产生不动点,对于 $[x ^ n]F(x)$ ,一种方案不动点有 $[x ^ {n - 1}]F(x) ^ 3$ 个,三种方案不动点有 $[x ^ {n - 1}] F(x) A(x)$ 个,两种方案不动点有 $[x ^ {n - 1}]B(x)$ 个。那么有 $F(x) = 1 + x \frac {F(x) ^ 3 + 3 F(x) A(x) + 2B(x)} 6$ 。考虑牛顿迭代,$G(F(x)) = 1 + x \frac {F(x) ^ 3 + 3 F(x) A(x) + 2B(x)} 6 - F(x) = 0$ ,$G’(F(x)) = x \frac {3F(x) ^ 2 + 3A(x)} 6 - 1$ ,那么有 $F(x)=F_0(x)-\frac{x(F_0(x)^3+3A(x)F_0(x)+2B(x))-6F_0(x)+6}{x(3F_0(x)^2+3A(x))-6}$ 。

查看代码
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#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('0' + x % 10);
}
typedef long long LL;
const int N = 1e6 + 10, mod = 998244353, inv2 = mod + 1 >> 1;
int rev[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 ntt (int *x, int bit, int op)
{
int tot = 1 << bit;
for (int i = 1; i < tot; ++i)
if ((rev[i] = rev[i >> 1] >> 1 | (i & 1) << bit - 1) > i)
swap(x[rev[i]], x[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;
}
void PolyMul (int n, int *f, int m, int *g, int nm, int *res)
{
int bit = 0;
while (1 << bit < n + m - 1) ++bit;
int tot = 1 << bit;
for (int i = n; i < tot; ++i) f[i] = 0;
for (int i = m; i < tot; ++i) g[i] = 0;
ntt(f, bit, 1), ntt(g, bit, 1);
for (int i = 0; i < tot; ++i)
res[i] = (LL)f[i] * g[i] % mod;
ntt(res, bit, -1);
for (int i = nm; i < tot; ++i) res[i] = 0;
}
void PolyInv(int n, int *x, int *g)
{
if (n == 1) return void(g[0] = binpow(x[0]));
int m = n + 1 >> 1;
int bit = 0;
while (1 << bit < n + m + m - 2) ++bit;
int tot = 1 << bit;
PolyInv(m, x, g);
for (int i = m; i < tot; ++i) g[i] = 0;
static int A[N];
for (int i = 0; i < n; ++i) A[i] = x[i];
for (int i = n; i < tot; ++i) A[i] = 0;
ntt(g, bit, 1), ntt(A, bit, 1);
for (int i = 0; i < tot; ++i)
g[i] = (2 - (LL)g[i] * A[i]) % mod * g[i] % mod;
ntt(g, bit, -1);
for (int i = n; i < tot; ++i) g[i] = 0;
}
void PolyCalc (int n, int *g)
{
if (n == 1) return void(g[0] = 1);
int m = n + 1 >> 1;
PolyCalc(m, g);
int bit = 0;
while (1 << bit < n + n - 1) ++bit;
int tot = 1 << bit;
static int A[N], B[N], C[N], D[N];
for (int i = 0; i < tot; ++i)
A[i] = i % 2 ? 0 : g[i / 2];
for (int i = 0; i < tot; ++i)
B[i] = i % 3 ? 0 : g[i / 3];
for (int i = 0; i < m; ++i) C[i] = g[i];
for (int i = m; i < tot; ++i) C[i] = 0;
ntt(C, bit, 1), ntt(A, bit, 1), ntt(B, bit, 1);
for (int i = 0; i < tot; ++i)
{
D[i] = ((LL)C[i] * C[i] % mod * C[i] % mod + 3ll * A[i] * C[i] % mod + 2 * B[i]) % mod;
A[i] = (3ll * C[i] * C[i] % mod + 3ll * A[i]) % mod;
}
ntt(D, bit, -1), ntt(A, bit, -1);
for (int i = n - 1; i; --i)
D[i] = (D[i - 1] - 6ll * g[i]) % mod, A[i] = A[i - 1];
D[0] = (6 - 6 * g[0]) % mod, A[0] = -6;
PolyInv(n, A, B);
PolyMul(n, D, n, B, n, A);
for (int i = 0; i < n; ++i) (g[i] -= A[i]) %= mod;
}
int main ()
{
static int n, A[N];
read(n);
PolyCalc(n + 1, A);
write((A[n] + mod) % mod);
return 0;
}