Official

C - Even XOR Editorial by evima


[1] Rephrasing the conditions

The condition is satisfied if and only if there is no non-empty subset of \(S\) whose size is even and the \(\mathrm{XOR}\) of whose elements is \(0\).


[2] Adding elements

To \(S\) satisfying the condition, what non-negative integer can we add so that the condition is still satisfied?

Let \(T\) be a subset of \(S\) whose size is odd, and \(X\) be the \(\mathrm{XOR}\) of the elements in \(T\).

If \(X\) is added to \(S\), the set resulting from adding \(X\) to \(T\) has an even size, and the \(\mathrm{XOR}\) of the elements in this set is \(0\), so the condition will be violated.

On the other hand, a non-negative integer \(Y\) that is never equal to the \(\mathrm{XOR}\) of the elements in an odd-sized subset of \(S\) can be added to \(S\) without violating the condition.

Additionally, let \(T\) and \(U\) be two odd-sized subsets of \(S\). If \(T\) and \(U\) differ, we can prove that their \(\mathrm{XOR}\)s of the elements also differ.

Indeed, if they were equal, the set \(V\) consisting of the elements that are contained in exactly one of \(T\) and \(U\) has an even size, and the \(\mathrm{XOR}\) of the elements in \(V\) is \(0\), contradicting the fact that \(S\) satisfies the condition.

Therefore, for every set \(S\) satisfying the condition, the number of non-negative integers that can be added to \(S\) without violating the condition is \(2^N - (\)the number of odd-sized subsets of \(S)\).


[3] Using dynamic programming

Let us consider a dynamic programming solution where \(\mathrm{dp}[i]:=(\)the number of sets of size \(i\) satisfying the condition\()\).

We have \(\mathrm{dp}[0]=1\). From the above argument, the transitions are:

\(\begin{cases} \mathrm{dp}[i]=\mathrm{dp}[i-1] \times 2^N\ (i=1)\\ \mathrm{dp}[i]=\frac{\mathrm{dp}[i-1] \times (2^N - 2^{i-2})}{i}\ (i \ge 2) \end{cases}\)

Here, the division by \(i\) comes from the need to consider the order in which the elements are added.

From the above formula, it can be seen that there is no set of size \(N+2\) or greater that satisfies the condition.

Therefore, the problem is solved in \(\mathrm{O}(N)\) time.

posted:
last update: