TITLE: Funky congruences
QUESTION [6 upvotes]: Suppose we have the remainders: {$(a^0_1, a^1_1), \ldots, (a^0_n, a^1_n)$} and the moduli {$c_1, \ldots, c_n$}. We want to know if there exists $b_1, \ldots, b_n \in$ {0,1} and $m \in \mathbb{N}$ such that:
$$
\begin{array}{lcl}
m & = & a^{b_1}_1 \quad (\mod c_1) \newline \\
  & = & a^{b_2}_2 \quad (\mod c_2) \newline \\
  & = & \ldots \newline \\
  & = & a^{b_n}_n \quad (\mod c_n) \newline \\
\end{array}
$$
That is basically a generalization of a system of linear congruences with an OR operator. Is there a nice necessary and sufficient condition for such an $m$ to exists?

REPLY [5 votes]: Here is an alternative approach, by reducing the problem to 2-SAT. It makes me ask: what if there were three values $(a_i^0, a_i^1, a_i^2)$ for each $i$ (and $b_i$'s in $\{0, 1, 2\}$)? It analogously can be reduced to some type of 2-SAT with ternary-valued variables. I don't know immediately whether either of these more general problems is NP-complete.
We need the following
Theorem: Suppose we have a collection of modular constraints on $m$, of the form $\{m \equiv v_i \; (\mod{c_i})\}_{i=1\ldots n}$. Then there is no $m$ meeting all constraints iff there are two particular constraints $1 \le j < k \le n$ so that no $m$ simultaneously satisfies those two constraints.
The proof is given later below. But, assuming the theorem, we just need to determine whether $b$ exists such that all pairs of two constraints $m \equiv a_j^{b_j} \; (\mod{c_j}), m \equiv a_k^{b_k} \; (\mod{c_k})$ are pairwise satisfiable. This can be done with 2-SAT: it is easy to check which pairs of $b^j, b^k$ allow a mutual solution (see Wadim's note in his first 2 lines), then for each pair $(j, k)$ we get up to 4 constraints depending on which pairs of values for $b_j, b_k$ can hold at the same time. For example if $a_j^0 = 5, c_j^0 = 20, a_k^0 = 6, c_k^0 = 42$, we see $b_j=0, b_k=0$ cannot both hold, since $m$ would have to be both even and odd. Thus we add the clause $(b^j = 1$ OR $b^k = 1)$ to our 2-SAT instance. Repeating this gives $O(n^2)$ clauses, then we run a linear-time 2-SAT algorithm to see if $b$ exists meeting all clauses, and are done.
Proof: $\Leftarrow$ is clear, so it suffices to prove the contrapositive of $\Rightarrow$. Thus, assume we can satisfy any given two constraints; we'll constrct an $m$ that satisfies all constraints. 
We focus on one prime $p$ at a time. Let $p^t$ be the maximum power of $p$ dividing any $b_i$, and let $\ell$ (which depends on $p$) be the maximizer, so $p^t | b_\ell$. The $m$ we hope to construct must satsify $m \equiv b_\ell \; (\mod{p^t})$, call this a crazy constraint. We get one crazy constraint for each prime dividing $c_1c_2\cdots c_n$. Moreover, for any $m$ that satisfies this crazy constraint, using Wadim's note twice, we deduce that $m \equiv v_i \; (\mod{\gcd(p^t, c_i)})$ for each $i$. But $\gcd(p^t, c_i)$ is just the highest power of $p$ dividing $c_i$. Thus by CRT, an $m$ which satisfies all crazy constraints (i.e. for all $p$), satisfies $m \equiv v_i \; (\mod{c_i})$, and hence all $n$ of the "input" constraints. But the crazy constraints are for relatively prime moduli, hence they  all simultanouesly hold for some $m$, and we are done.