Problems > System of Remainders > Editorial

System of Remainders — Solution & Editorial

Back to the Problem

Merge congruences pairwise with the general CRT: a solution to x≡r0 (mod m0) and x≡ri (mod mi) exists iff gcd(m0, mi) divides (ri − r0); combine into a single congruence modulo lcm(m0, mi).

Complexity: O(K log)

Watch out: Non-coprime moduli — the divisibility feasibility check is where naive CRT silently produces garbage.