Contest Duration: - (local time) (270 minutes) Back to Home

C - Flipper Editorial by rng58_admin

First, let’s find some invariants.

• For each column $$j$$, define $$p_j$$ as the parity of number of black cells in the column. This is an invariant.
• For each row $$i$$, define $$s_i$$ as follows. $$s_i$$ is a string of length $$3$$, and each character of it is either 0 or 1. $$k$$-th character represents the parity of number of black cells among the cells $$(i, k), (i, k+3), (i, k+6), \cdots$$. Then, whenever we perform an operation involving this row, all three characters of $$s_i$$ is flipped. Thus, for example, if initially $$s_i$$ is 010, it may be changed to 101, but nothing else.

We can prove that the “invariants” above are sufficient. That is, if we have two configurations (i.e., a set of black cells in the large grid) with the same invariants, we can convert one to the other. This is because we can always match a subrectangle of dimensions $$999999999 \times 999999998$$ (by turning the color of each cell one by one properly), and the invariants above uniquely determines the colors of the remaining cells.

Next, for a given values of $$p_j$$ and $$s_i$$, let’s compute the minimum possible number of black cells.

• Let $$t_k (1 \leq k \leq 3)$$ be the number of odd numbers among $$p_k, p_{k+3}, p_{k+6}, \cdots$$. Let $$S_k (1 \leq k \leq 3)$$ be the sum of $$s_{i,k}$$ over all $$i$$. Then, the parity of $$t_k$$ and $$S_k$$ must match (otherwise we get a contradiction). If all parity matches, the optimal number of black cells is $$\sum \min \{ S_k, t_k \}$$.

Since the values of $$t_k$$ are given in the input, our task is to change the values of $$S_k$$ (by changing 010 to 101 for example) and minimize the value above.

To summarize,

• We are given some number of vectors in a $$3$$-dimensional space. Each vector is one of $$(0, 0, 0), (0, 0, 1), (0, 1, 0), (1, 0, 0)$$.
• We are allowed to change some vectors: $$(0, 0, 0) \rightarrow (1, 1, 1), (0, 0, 1) \rightarrow (1, 1, 0), (0, 1, 0) \rightarrow (1, 0, 1), (1, 0, 0) \rightarrow (0, 1, 1)$$.
• You are given three constants $$x, y, z$$.
• Let $$(X, Y, Z)$$ be the sum of all vectors after the changes. Then your task is to satisfy $$x \equiv X, y \equiv Y, z \equiv Z \pmod{2}$$, and minimize $$\max \{ x, X \} + \max \{ y, Y \} + \max \{ z, Z \}$$.

To solve this problem, first let’s fix the parity of number of changes of each type (if we decide to perform odd number of operations, perform it once for now). Then, notice that it is never optimal to perform two or more types of operations from now. We can try all possibilities with one or less types of operations.

posted:
last update: