結果

問題 No.2117 中国剰余定理入門
コンテスト
ユーザー norioc
提出日時 2026-06-01 01:35:48
言語 PyPy3
(7.3.17)
コンパイル:
pypy3 -mpy_compile _filename_
実行:
pypy3 _filename_
結果
AC  
実行時間 26 ms / 2,000 ms
コード長 732 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 382 ms
コンパイル使用メモリ 85,056 KB
実行使用メモリ 52,480 KB
最終ジャッジ日時 2026-06-01 01:35:50
合計ジャッジ時間 2,067 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge2_1
純コード判定待ち
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 20
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

from math import gcd


def linear_congruence(a: int, b: int, m: int) -> tuple[int, int] | tuple[None, None]:
    # a x = b (mod m)
    g = gcd(a, m)
    if b % g != 0:
        return None, None

    a2 = a // g
    b2 = b // g
    m2 = m // g
    # a' x = b' (mod m')
    x = b2 * pow(a2, -1, m2) % m2
    return x, m2


def crt2(r1, m1, r2, m2):
    # x = r1 (mod m1)
    # x = r1 + m1 t
    # r1 + m1 t = r2 (mod m2)
    # m1 t = r2 - r1 (mod m2)

    t, m = linear_congruence(m1, r2 - r1, m2)
    if t is None:
        return None

    x = r1 + m1 * t
    return x


B0, C0 = map(int, input().split())
B1, C1 = map(int, input().split())

ans = crt2(C0 % B0, B0, C1 % B1, B1)
if ans is None:
    print('NaN')
else:
    print(ans)
0