CRT Chinese Remainder Theorem

互いに素なについて の時、 を満たす がただ一つ存在する。

解の存在

def crt(a, m, b, n):
    """
    Find x s.t. x % m == a and x % n == b
    >>> crt(2, 3, 1, 5)
    11
    >>> crt(1, 4, 3, 6)
    9
    """
    x, y, g = extended_euclidean(m, n)
    if g == 1:
        return (b * m * x + a * n * y) % (m * n)
    s = (b - a) // g
    return (a + s * m * x) % (m * n // g)
  • src
  • たる を拡張ユークリッド互除法で求める
  • なのでは条件を満たす

解の一意性などはこちら