跳到正文

题解

Solution of AT_abc212_h [ABC212H] Nim Counting

tder约 3 分钟
原文
文章信息与版本

文章信息

发布
2024年11月10日
收录
2025年12月12日
更新
2026年11月10日
篇幅
约 3 分钟

版本

  1. v1当前

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

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

fi,j←∑fi−1,j⊕Ak⋅f1,Akf_{i,j}\gets\sum f_{i-1,j\oplus A_k}\cdot f_{1,A_k}

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

Consider optimisation. Let p(j)=f1,jp(j)=f_{1,j}, q(j)=fi−1,jq(j)=f_{i-1,j}, and g(j)=fi,jg(j)=f_{i,j}, then we have:

g(x)=∑a⊕b=xp(x)⋅q(x)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 g=p*q. Thus, when i=ni=n, there is g=qng=q^n. The complexity O(K⋅Vlog⁡V) \mathcal{O}(K\cdot V\log V), the bottleneck is to do KK FWTs.

Consider the optimisation. The desired is:

ans=∑i=1K∑j=1V−1qji=∑j=1V−1∑i=1Kqji\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 FWT(X+Y)=FWT(X)+FWT(Y)\text{FWT}(X+Y)=\text{FWT}(X)+\text{FWT}(Y) and FWT(a⋅X)=a⋅FWT(X)\text{FWT}(a\cdot X)=a\cdot\text{FWT}(X). Therefore, according to the formula for the sum of an isoperimetric series, there are:

∑i=1Kqji=qjK−1qj−1⋅qj\sum_{i=1}^K{q_j}^i=\frac{{q_j}^K-1}{q_j-1}\cdot q_j

The answer is namely:

ans=∑j=1V−1∑i=1Kqji=∑j=1V−1qjK−1qj−1⋅qj\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 O(Vlog⁡V)\mathcal{O}(V\log V).

原文