公式

E - Shift and XOR Switches 解説 by evima


In fact, if the set of switches pressed is the same, the order in which they are pressed does not change the resulting \(B\). Let the values written on the pressed switches be \(C_1, C_2, \cdots, C_K\). Then, \(B\) can be interpreted in either of the following ways:

  • \(B_i\) is the parity of the number of ways to choose some of the \(C_i\) so that they sum to \(i\), or
  • \(B = \prod{(1 + x^{C_i})} \pmod {x^{N}}\) as a polynomial over \(\mathbb F_2\).

Under either interpretation, we can see that the order in which the switches are pressed has no effect on the resulting \(B\).

In addition, there are two important facts. The first is as follows.

  • Pressing two switches with \(X\) written on them is equivalent to pressing one switch with \(2X\) written on it.

This can be justified by considering \((x + 1)^2 = x^2 + 1 \pmod 2\). By repeating this transformation, the set of values of the pressed switches can be transformed so that they are distinct.

The second important fact is as follows.

  • If it is known that the values of the pressed switches are distinct, the set of switch values can be recovered from the resulting \(B\).

This can be done by repeating the following:

  • From the smallest \(i\) such that \(B_i = 1\), find the smallest value among the pressed switches.
  • Compute, via FPS division (a DP that undoes the operation), what would happen “if this switch had not been pressed.”

These two facts suggest that “the set obtained after transforming the pressed switch values to be distinct characterizes the resulting \(B\).” That is, this problem can be restated as follows:

  • Choose some elements from \(A\) and call this set \(C\).
  • If \(C\) contains two or more copies of the same value \(x\), remove two of them, and instead add \(2x\) if \(2x \lt N\).
  • Repeat this operation as long as it is possible. How many possible resulting sets are there?

Let us consider this problem. First, we can observe that we may classify the values of \(A\) by their odd part, that is, the value obtained by dividing \(A_i\) by \(2\) as many times as possible. Each class can be solved independently.

After this classification, the problem becomes: given some numbers, each of which is among \(1, 2, 4, 8, \ldots, 2^{L-1}\), how many distinct values that are at most \(2^{L}\) can be formed as their sum?

Consider the decision problem of whether a fixed target value is achievable. This can be solved greedily: look at the bits from the lowest one, and if a bit is \(1\), use one corresponding switch. Then, pair up all remaining switches into groups of two and convert them into switches of twice the value.

Based on this greedy method, we can implement a digit DP that determines the final value from the lowest bit upward. It is important that the number of switches propagated has at most two possible values at each digit, which makes this run in \(O(L)\) time.

The sum of \(L\) over all classes is \(N\), so this problem can be solved in a total of \(O(N + M)\) time.

投稿日時:
最終更新: