Generalized Chinese remainder theorem

Write codeHard35 min

Your task

You are given two equal-length lists, remainders and moduli, describing a system of congruences x ≡ remainders[i] (mod moduli[i]) for each i. The moduli are positive but NOT necessarily pairwise coprime. Return the smallest non-negative x satisfying all congruences, or -1 if the system is inconsistent. For example, x ≡ 2 (mod 3), x ≡ 3 (mod 5), x ≡ 2 (mod 7) yields 23. All values fit in 64-bit integers.

Examples

crt(remainders: list[int], moduli: list[int]) -> int
Example 1
Inputcrt([2, 3, 2], [3, 5, 7])
Output23