Dyd's Blog

He who has a strong enough why can bear almost any how.

CF1119H Triple

FWT + 解方程 高级版

Triple

题意

给定常数 $k \le 17$ 和常数 $x, y, z \le 10^9$ ,现在有 $n \le 10^5$ 个数组,对于每个数组,给定 $a_i, b_i, c_i \le 2^k$ ,表示该数组有 $x$ 个 $a_i$ , $y$ 个 $b_i$ , $z$ 个 $c_i$ ,现在从每个数组中选一个数字异或起来得到 $sum$ ,对于每个 $sum \in [0, 2^k)$ ,输出方案数

思路

先考虑暴力 FWT :构造幂级数 $f_i(t) = xt^{a_i} + yt^{b_i} + zt^{c_i}$ ,定义幂级数乘法为异或卷积,那么设 $F = \prod f_i$ ,则 $F[i]$ 即为 $sum = i$ 的方案数,直接暴力 FWT 是 $O(nk2^k)$

任然考虑手玩一下 FWT ,记幂级数 $f’ = FWT(f)$ ,有 $f’[s] = (-1)^{\mid a_i \& s\mid} x + (-1)^{\mid b_i \& s\mid} y + (-1)^{\mid c_i \& s\mid} z$ $f’[s] = (-1)^{\mid a_i & s\mid} x + (-1)^{\mid b_i & s\mid} y + (-1)^{\mid c_i & s\mid} z$

这有点痛苦,考虑把三元组 $(a_i, b_i, c_i)$ 变成 $(a_i \oplus c_i, b_i \oplus c_i, 0)$ ,记 $C = c_1 \oplus c_2 \oplus … \oplus c_n$ ,最后 $sum$ 的答案存在 $sum \oplus C$ 处;以下的讨论中,$a, b, sum$ 都是已经变换了的

那么 $f’[s] = (-1)^{\mid a_i \& s\mid} x + (-1)^{\mid b_i \& s\mid} y + z$ $f’[s] = (-1)^{\mid a_i & s\mid} x + (-1)^{\mid b_i & s\mid} y + z$ ,就只有四种情况:

  1. $x + y + z$
  2. $x - y + z$
  3. $-x + y + z$
  4. $-x - y + z$

不妨记以上四种情况出现的次数分别为 $d_1, d_2, d_3, d_4$ ,可得: $F’[s] = (x + y + z)^{d_1}(x - y + z)^{d_2}(-x + y + z)^{d_3}(-x - y + z)^{d_4}$ ,于是问题变成求 $d_1, d_2, d_3, d_4$

考虑解方程,我们现在有 $d_1 + d_2 + d_3 + d_4 = n$ ,还要再找到 $3$ 个方程,我们使用 FWT 找方程:

  1. 构造 $G(t) = \sum_{i = 1}^{n} t^{a_i}$ ,那么 $G’[s] = (-1)^{\mid s \& a_i \mid}$ $G’[s] = (-1)^{\mid s & a_i \mid}$ ,即只有 $x$ 符号的贡献,那么有 $G’[s] = d_1 + d_2 - d_3 - d_4$ ,不妨记作 $d_1 + d_2 - d_3 - d_4 = p_1$
  2. 同理构造 $G(t) = \sum_{i = 1}^{n} t^{b_i}$ ,有 $G’[s] = d_1 + d_3 - d_2 - d_4$ 不妨记作 $d_1 - d_2 + d_3 - d_4 = p_2$
  3. 最后再构造 $G(t) = \sum_{i = 1}^{n} t^{a_i \oplus b_i}$ ,有 $G’[s] = d_1 + d_4 - d_2 - d_3$ ,不妨记作 $d_1 - d_2 - d_3 + d_4 = p_3$

于是我们有:
$$
\begin{cases}
d_1 + d_2 + d_3 + d_4 = n \\
d_1 + d_2 - d_3 - d_4 = p_1 \\
d_1 - d_2 + d_3 - d_4 = p_2 \\
d_1 - d_2 - d_3 + d_4 = p_3
\end{cases}
$$
手玩解得:
$$
\begin{cases}
d_1 = \frac{n + p_1 + p_2 + p_3}{4} \\
d_2 = \frac{n + p_1 - p_2 - p_3}{4} \\
d_3 = \frac{n - p_1 + p_2 - p_3}{4} \\
d_4 = \frac{n - p_1 - p_2 + p_3}{4}
\end{cases}
$$
时间复杂度 $O(k2^k)$

代码

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
#include <bits/stdc++.h>
using LL = long long;
const int N = 1e5 + 100, K = 20, P = 998244353, iv2 = 499122177, iv4 = 748683265;
int n, tot, C;
int a[N], b[N], c[N], g[N << 1], f[N << 1];
std::array<int, 4> d[N << 1];
void adj(int &x){ x += (x >> 31) & P; }
int qpow(int x, int y)
{
int res = 1;
for (; y; y >>= 1, x = LL(x) * x % P) if (y & 1) res = LL(res) * x % P;
return res;
}
void fwtor(int x[], int len)
{
for (int i = 2, t1, t2; i <= len; i <<= 1)
for (int mid = i >> 1, j = 0; j < len; j += i)
for (int k = j; k < j + mid; ++k)
{
t1 = x[k], t2 = x[k + mid];
adj(x[k] = t1 + t2 - P), adj(x[k + mid] = t1 - t2);
}
}
void ifwtor(int x[], int len)
{
for (int i = 2, t1, t2; i <= len; i <<= 1)
for (int mid = i >> 1, j = 0; j < len; j += i)
for (int k = j; k < j + mid; ++k)
{
t1 = x[k], t2 = x[k + mid];
adj(x[k] = t1 + t2 - P), adj(x[k + mid] = t1 - t2);
x[k] = LL(iv2) * x[k] % P, x[k + mid] = LL(iv2) * x[k + mid] % P;
}
}
int main()
{
scanf("%d %d", &n, &tot), tot = 1 << tot;
int u, v, w, t0, t1, t2, t3;
scanf("%d %d %d", &u, &v, &w);
t0 = (LL(u) + v + w) % P;
t1 = ((LL(u) - v + w ) % P + P) % P;
t2 = ((LL(-u) + v + w) % P + P) % P;
t3 = ((LL(-u) - v + w) % P + P) % P;
for (int i = 1; i <= n; ++i)
{
scanf("%d %d %d", &a[i], &b[i], &c[i]);
a[i] ^= c[i], b[i] ^= c[i], C ^= c[i];
}
for (int i = 0; i <= tot; ++i) d[i] = {n, n, n, n};
for (int i = 1; i <= n; ++i) adj(++g[a[i]]);
fwtor(g, tot);
for (int i = 0; i <= tot; ++i)
{
adj(d[i][0] += g[i] - P);
adj(d[i][1] += g[i] - P);
adj(d[i][2] -= g[i]);
adj(d[i][3] -= g[i]);
}
memset(g, 0, (tot + 1) << 2);
for (int i = 1; i <= n; ++i) adj(++g[b[i]]);
fwtor(g, tot);
for (int i = 0; i <= tot; ++i)
{
adj(d[i][0] += g[i] - P);
adj(d[i][1] -= g[i]);
adj(d[i][2] += g[i] - P);
adj(d[i][3] -= g[i]);
}
memset(g, 0, (tot + 1) << 2);
for (int i = 1; i <= n; ++i) adj(++g[a[i] ^ b[i]]);
fwtor(g, tot);
for (int i = 0; i <= tot; ++i)
{
adj(d[i][0] += g[i] - P);
adj(d[i][1] -= g[i]);
adj(d[i][2] -= g[i]);
adj(d[i][3] += g[i] - P);
}
for (int i = 0; i <= tot; ++i)
{
d[i][0] = LL(iv4) * d[i][0] % P;
d[i][1] = LL(iv4) * d[i][1] % P;
d[i][2] = LL(iv4) * d[i][2] % P;
d[i][3] = LL(iv4) * d[i][3] % P;
}
for (int i = 0; i <= tot; ++i) f[i] = LL(qpow(t0, d[i][0])) * qpow(t1, d[i][1]) % P * qpow(t2, d[i][2]) % P * qpow(t3, d[i][3]) % P;
ifwtor(f, tot);
for (int i = 0; i < tot; ++i) printf("%d ", f[i ^ C]);
return 0;
}