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