The Royal Messenger of Prime City carries a sealed scroll with three congruences that must all be satisfied simultaneously. The scroll reads: x Ôëí r1 (mod m1), x Ôëí r2 (mod m2), x Ôëí r3 (mod m3). The three moduli are pairwise coprime, and the messenger must find the smallest positive x that satisfies all three. "The Chinese Remainder Theorem guarantees a unique solution modulo the product of the three moduli," the messenger says. "Find it, and the scroll's secret shall be revealed." Given three congruences defined by (r1, m1), (r2, m2), (r3, m3) where the moduli are pairwise coprime, find the smallest non-negative x satisfying all three. Constraints: 1 <= ri < mi <= 10^6, mi are pairwise coprime Input: 2 3 3 5 2 7 Output: 23 Input: 1 2 2 3 3 5 Output: 23
Constraints:
1 <= ri < mi <= 10^6, pairwise coprime
Tags:
