Solution of AT_abc212_h [ABC212H] Nim Counting

· · 题解

你可以浏览这个题解的中文版本。

Consider a plain dp, f_{i,j} denoting the number of scenarios with i selected and an xor sum of j. There are transfers:

f_{i,j}\gets\sum f_{i-1,j\oplus A_k}\cdot f_{1,A_k}

In particular, f_{1,j}\gets\sum[A_k=j]. Complexity \mathcal{O}(N\cdot K\cdot V).

Consider optimisation. Let p(j)=f_{1,j},

$$ g(x)=\sum_{a\oplus b=x} p(x)\cdot q(x) $$ It is easy to see that this is an xor convolution, i.e. $$ g=p*q$$. Thus, when $i=n$, there is $g=q^n$. The complexity $$ \mathcal{O}(K\cdot V\log V)$$, the bottleneck is to do $K$ FWTs. Consider the optimisation. The desired is: $$ \begin{aligned} \text{ans}&=\sum_{i=1}^K\sum_{j=1}^{V-1}{q_j}^i \\ &=\sum_{j=1}^{V-1}\sum_{i=1}^K{q_j}^i \end{aligned} $$ The key point of this question is that FWT has ‘linearity’, i.e. it satisfies $\text{FWT}(X+Y)=\text{FWT}(X)+\text{FWT}(Y)$ and $\text{FWT}(a\cdot X)=a\cdot\text{FWT}(X)$. Therefore, according to the formula for the sum of an isoperimetric series, there are: $$ \sum_{i=1}^K{q_j}^i=\frac{{q_j}^K-1}{q_j-1}\cdot q_j $$ The answer is namely: $$ \begin{aligned} \text{ans}&=\sum_{j=1}^{V-1}\sum_{i=1}^K{q_j}^i \\ &=\sum_{j=1}^{V-1}\frac{{q_j}^K-1}{q_j-1}\cdot q_j \end{aligned} $$ In this way, it is sufficient to do the FWT only once. Complexity $\mathcal{O}(V\log V)$.