from sympy.ntheory.modular import crt as sympy_crt


def _crt(m1, m2, v1, v2):
    """
    Chinese Remainder Theorem

    Returns x such that x = v1 (mod m1) and x = v2 (mod m2), or None if no
    such solution exists.

    """
    # Avoid calling sympy_crt in the cases where the inputs would be arrays.
    if m1 == 1:
        return v2 % m2
    if m2 == 1:
        return v1 % m1

    # Only import SymPy when necessary

    res = sympy_crt([m1, m2], [v1, v2])
    if res is None:
        return res
    # Make sure the result isn't a gmpy2.mpz
    return int(res[0])