Little Pony and Elements of Harmony

题目描述

The Elements of Harmony are six supernatural artifacts representing subjective aspects of harmony. They are arguably the most powerful force in Equestria. The inside of Elements of Harmony can be seen as a complete graph with $n$ vertices labeled from $0$ to $n-1$, where $n$ is a power of two, equal to $2^m$.

The energy in Elements of Harmony is in constant movement. According to the ancient book, the energy of vertex $u$ in time $i$ ($e_i[u]$) equals to:

$$
e_i[u]=\sum_v e_{i-1}[v] \cdot b[f(u, v)]
$$

Here $b[]$ is the transformation coefficient - an array of $m+1$ integers and $f(u, v)$ is the number of ones in the binary representation of number ($x \oplus y$).

Given the transformation coefficient and the energy distribution at time $0$ ($e_0[]$). Help Twilight Sparkle predict the energy distribution at time $t$ ($e_t[]$). The answer can be quite large, so output it modulo $p$.

题意概述

给定$m$。令$n=2^m$,$f(x, y)$表示$x \oplus y$(异或)在二进制下$1$的个数。给定一个长为$n$的数组$e_0$和一个长为$(m+1)$的数组$b$。有递推关系$e_i[u]=\sum_v e_{i-1}[v] \cdot b[f(u, v)]$。求$e_t$在模$p$意义下的值。

数据范围:$1 \le m \le 20, \ 0 \le t \le 10^{18}, \ 2 \le p \le 10^9, \ 1 \le e_0[i] \le 10^9, \ 0 \le b[i] \le 10^9$。

算法分析

令$g(x)$表示$x$在二进制下$1$的个数,$c[i]=b[g(i)]$。那么递推式可以表示为

$$
\begin{align}
e_i[u]&=\sum_v e_{i-1}[v] \cdot c[u \oplus v] \\
e_i[u \oplus v]&=\sum_{u, v} e_{i-1}[u] \cdot c[v]
\end{align}
$$

这是一个异或卷积的式子,可以用 FWT 快速求值。由于逆 FWT 时需要除以$2^m$,而在模$p$意义下$2^m$可能不存在逆元,所以需要在模$2^mp$意义下计算,最后将结果除以$2^m$。

代码

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
/*
* The light at the end of the tunnel is the headlight of an approaching train.
*/

#include <cstdio>

#define int long long

void read(int &n) {
char c;
for (; (c = getchar()) < '0' || c > '9';)
;
for (n = c - '0'; (c = getchar()) >= '0' && c <= '9'; (n *= 10) += c - '0')
;
}

static const int N = 1100000;
int p, b[25], c[N], e[N], f[N];

int mul(int a, int b) {
return (a * b - (int)(((double)a * b + 0.5) / p) * p) % p;
}

int power(int a, int b) {
int ret = 1;
for (; b; b >>= 1)
b & 1 && (ret = mul(ret, a)), a = mul(a, a);
return ret;
}

void fwt(int *a, int n) {
for (int i = 1; i < n; i <<= 1)
for (int j = 0; j < n; j += i << 1)
for (int k = 0; k < i; ++k) {
int x = a[j + k], y = a[j + k + i];
a[j + k] = (x + y) % p, a[j + k + i] = (p + x - y) % p;
}
}

signed main() {
int n, m, t;
read(m), read(t), read(p), n = 1 << m, p <<= m;
for (int i = 0; i < n; ++i)
read(e[i]), f[i] = f[i >> 1] + (i & 1);
for (int i = 0; i <= m; ++i)
read(b[i]);
for (int i = 0; i < n; ++i)
c[i] = b[f[i]];
fwt(e, n), fwt(c, n);
for (int i = 0; i < n; ++i)
e[i] = mul(e[i], power(c[i], t));
fwt(e, n);
for (int i = 0; i < n; ++i)
printf("%lld\n", e[i] >> m);
return 0;
}

Little Pony and Elements of Harmony
https://regmsif.cf/2018/01/29/oi/little-pony-and-elements-of-harmony/
作者
RegMs If
发布于
2018年1月29日
许可协议