你可以浏览这个题解的中文版本。
Consider a plain dp, fi,j denoting the number of scenarios with i selected and an xor sum of j. There are transfers:
fi,j←∑fi−1,j⊕Ak⋅f1,Ak
In particular, f1,j←∑[Ak=j]. Complexity O(N⋅K⋅V).
Consider optimisation. Let p(j)=f1,j,
q(j)=fi−1,j, and g(j)=fi,j, then we have:
g(x)=a⊕b=x∑p(x)⋅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=qn. The complexity O(K⋅VlogV), the bottleneck is to do K FWTs.
Consider the optimisation. The desired is:
ans=i=1∑Kj=1∑V−1qji=j=1∑V−1i=1∑Kqji
The key point of this question is that FWT has ‘linearity’, i.e. it satisfies FWT(X+Y)=FWT(X)+FWT(Y) and FWT(a⋅X)=a⋅FWT(X). Therefore, according to the formula for the sum of an isoperimetric series, there are:
i=1∑Kqji=qj−1qjK−1⋅qj
The answer is namely:
ans=j=1∑V−1i=1∑Kqji=j=1∑V−1qj−1qjK−1⋅qj
In this way, it is sufficient to do the FWT only once. Complexity O(VlogV).