Official

F - Coprime Solitaire Editorial by en_translator


We consider boolean variables \(W_1, W_2, \ldots, W_N\) that can be either \(0\) or \(1\). Also, for each \(i = 1, 2, \ldots, N\), we regard that \(W_i =1\) corresponds to the Card \(i\) placed face up, and \(W_i = 0\) to face down.

Generally, a boolean variable \(W\) and its negation \(\lnot W\) is called literal. In this editorial, for Card \(i\) we define “the literal corresponding to the front” as \(W_i\) and “the literal corresponding to the back” as \(\lnot W_i\). Additionally, for each prime \(p\), we denote by \(X_{p, 1}, X_{p, 2}, \ldots, X_{p, k_p}\) the sequence of all the literals corresponding to the faces, each of whose number written has a prime factor \(p\). (That is, each of \(X_{p, 1}, X_{p, 2}, \ldots, X_{p, k_p}\) stores either of \(W_1, \lnot W_1, W_2, \lnot W_2, \ldots, W_N, \lnot W_N\).)

Takahashi is happy if and only if “for every prime \(p \,\,(\leq 2 \times 10^6)\), there is at most one integer placed face up with the prime factor \(p\).” This correspond to the conditions where “for every prime \(p \,\,(\leq 2 \times 10^6)\), at most one of \(X_{p, 1}, X_{p, 2}, \ldots, X_{p, k_p}\) has the value \(1\).”
The condition where “at most one of \(X_{p, 1}, X_{p, 2}, \ldots, X_{p, k_p}\) has the value \(1\)” is equivalent to the conditions as follow:

at most one of \(X_{p, 1}, X_{p, 2}, \ldots, X_{p, k_p}\) has the value \(1\)
\(\Leftrightarrow\) for every pair \((i, j)\) such that \(1 \leq i < j \leq k_p\), at most one of \(X_{p, i}, X_{p, j}\) has the value \(1\).
\(\Leftrightarrow\) for every pair \((i, j)\) such that \(1 \leq i, j \leq k_p\) and \(i \neq j\), if \(X_{p, i} = 1\) then \(X_{p, j} = 0\). \(\Leftrightarrow\) for every pair \((i, j)\) such that \(1 \leq i < j \leq k_p\), the value of the boolean expression \((X_{p, i} \rightarrow \lnot X_{p, j})\) is \(1\). (Here, \(X \rightarrow Y\) means \(\lnot X \lor Y\).)
\(\Leftrightarrow\) The boolean expression \(F_p := \displaystyle\bigwedge_{1 \leq i, j \leq k_p, i \neq j} (X_{p, i} \rightarrow \lnot X_{p, j})\) has the value \(1\).

Therefore, Takahashi is happy if and only if for every prime \(p \,\,(\leq 2 \times 10^6)\) the boolean expression \(F_p\) described above has the value \(1\); namely,

the logical expression \(F := \displaystyle\bigwedge_{p:\text{prime}, p \leq 2\times 10^6} F_p = \displaystyle\bigwedge_{p:\text{prime}, p \leq 2\times 10^6} \displaystyle\bigwedge_{1 \leq i, j \leq k_p, i \neq j} (X_{p, i} \rightarrow \lnot X_{p, j})\)

has the value \(1\).
Therefore, this problem boils down to whether we can determine which card to flip so that the boolean expression \(F\) has the value \(1\), that is, whether the logical expression \(F\) can be fulfilled or not. Such problem is called “Satisfiability Problem” (SAT).

In general, a concatenation of a finite number of literals by logical disjunctions \(\lor\) is called a clause.
While general effective solution for SAT is unknown, the SAT defined by a concatenation by logical disjunctions \(\lor\) of \(M\) clauses, each of which consists of at most \(2\) literal, or 2-SAT, can be solved in a total of \( \mathrm{O}(M)\) time.
The algorithm of solving \(2-SAT\) is available via the implementation in AtCoder Library (ACL). (We delegate the details of algorithms of solving 2-SAT to other materials.)
Since every clause of \(F\) consists of two literals (note that \(X \rightarrow Y\) denotes \(\lnot X \lor Y\)), this problem can be solved as a 2-SAT problem.
However, for a prime \(p \,\,(\leq 2 \times 10^6)\), \(F_p\) has at most \(2N(2N-1)\) clauses, so we cannot finish this in the time limit.

Now, we consider reducing the number of clauses in \(F_p\).
First, we add supplementary boolean variables \(Y_{p, 1}, Y_{p, 2}, \ldots, Y_{p, k_p}\) and \(Z_{p, 1}, Z_{p, 2}, \ldots, Z_{p, k_p}\). And we define an substitutional boolean expression \(F'_p\) instead of \(F_p\) as

\(F'_p := \displaystyle\bigwedge_{1 \leq i \leq k_p} (Y_{p, i} \rightarrow \lnot X_{p, i}) \land \displaystyle\bigwedge_{2 \leq i \leq k_p} (Y_{p, i} \rightarrow Y_{p, i-1}) \land \displaystyle\bigwedge_{2 \leq i \leq k_p} (X_{p, i} \rightarrow Y_{p, i-1}) \land \displaystyle\bigwedge_{1 \leq i \leq k_p} (Z_{p, i} \rightarrow \lnot X_{p, i}) \land \displaystyle\bigwedge_{1 \leq i \leq k_p-1} (Z_{p, i} \rightarrow Z_{p, i+1}) \land \displaystyle\bigwedge_{1 \leq i \leq k_p-1} (X_{p, i} \rightarrow Z_{p, i+1})\)

and let \(F' := \displaystyle\bigwedge_{p:\text{prime}, p \leq 2\times 10^6} F_p\), then

\(F'\) is satisfiable \(\Leftrightarrow\) \(F\) is satisfiable.

Therefore, solving the 2-SAT for the boolean expression \(F\) is equivalent to solving the 2-SAT for the boolean expression \(F'\).
For every prime \(p\), \(F'_p\) has \(\mathrm{O}(k_p)\) number of clauses, so the number of clauses in \(F'\) is \(\mathrm{O}(\sum_{p:\text{prime}, p \leq 2\times 10^6}k_p) = \mathrm{O}(N \log L)\). (Here, \(L\) is the maximum value of \(A_i, B_i\))
Thus, we can find the answer for the original problem by solving the 2-SAT against the boolean expression \(F'\).

posted:
last update: