結果

問題 No.2558 中国剰余定理
ユーザー yaakiyuyaakiyu
提出日時 2023-12-02 14:34:44
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 784 bytes
コンパイル時間 186 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 65,960 KB
最終ジャッジ日時 2023-12-02 14:35:30
合計ジャッジ時間 2,627 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 RE -
testcase_02 WA -
testcase_03 RE -
testcase_04 WA -
testcase_05 RE -
testcase_06 WA -
testcase_07 RE -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

import math


def CRT(rem_pair, mod_pair):
    """
        rem_pair = (r1,r2)
        mod_pair = (m1,m2) とします。この時m1,m2がどちらも0ではないとします。
        この時

        x = m1 (mod r1)
        x = m2 (mod r2)

        となる整数xが存在する場合、0以上の最小の自然数を返します。
        存在しない場合は-1を返します。
    """
    r1, r2 = rem_pair
    m1, m2 = mod_pair

    assert (m1 != 0 and m2 != 0)
    assert len(rem_pair) == len(mod_pair) == 2
    g = math.gcd(m1, m2)
    if (r2 - r1) % g != 0:
        return -1
    M1, M2, R = m1//g, (-m2)//g, (r2 - r1)//g

    inv = pow(M1, -1, M2) * R % M2

    return (m1*inv + r1) % abs(m1*m2//g)

A,B,a,b = map(int, input().split())
print(CRT((A,B), (a, b)))
0