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$.

算法分析

\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}

代码

/*
* The light at the end of the tunnel is the headlight of an approaching train.
*/

#include <cstdio>

#define int long long

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;
for (int i = 0; i < n; ++i)
read(e[i]), f[i] = f[i >> 1] + (i & 1);
for (int i = 0; i <= m; ++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;
}


418 I'm a teapot