結果

問題 No.186 中華風 (Easy)
ユーザー tktk_snsntktk_snsn
提出日時 2021-03-25 23:59:23
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
WA  
実行時間 -
コード長 1,013 bytes
コンパイル時間 90 ms
コンパイル使用メモリ 10,928 KB
実行使用メモリ 7,984 KB
最終ジャッジ日時 2023-08-18 06:17:54
合計ジャッジ時間 1,981 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 15 ms
7,784 KB
testcase_01 AC 15 ms
7,804 KB
testcase_02 AC 16 ms
7,860 KB
testcase_03 AC 15 ms
7,856 KB
testcase_04 AC 15 ms
7,812 KB
testcase_05 AC 16 ms
7,800 KB
testcase_06 AC 16 ms
7,848 KB
testcase_07 AC 16 ms
7,984 KB
testcase_08 AC 16 ms
7,800 KB
testcase_09 AC 15 ms
7,912 KB
testcase_10 AC 16 ms
7,864 KB
testcase_11 AC 15 ms
7,916 KB
testcase_12 AC 16 ms
7,848 KB
testcase_13 AC 15 ms
7,960 KB
testcase_14 AC 16 ms
7,940 KB
testcase_15 AC 15 ms
7,796 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 AC 15 ms
7,960 KB
testcase_19 AC 16 ms
7,912 KB
testcase_20 AC 16 ms
7,920 KB
testcase_21 AC 15 ms
7,812 KB
testcase_22 AC 15 ms
7,892 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def extgcd(a, b):
    """
    拡張Euclidの互除法
    INPUT:
        a, b
    OUTPUT:
        d: gcd(a, b)
        (x, y): ax + by = d の解
    """
    if b == 0:
        return a, 1, 0
    d, y, x = extgcd(b, a % b)
    y -= a // b * x
    return d, x, y


def crt(B, M):
    """
    中国剰余定理
    x ≡ b0 (mod M0)
    x ≡ b1 (mod M1)
    x ≡ b2 (mod M2)
    となるxを求める
    INPUT:
        B:[b0,b1,..]
        M:[m0,m1,..]
    OUTPUT:
        (x, mod) -> x:最小の答え(0<=x<mod)
                    mod:lcm(m0,m1,..)
        (0,-1) -> 解がない場合
    """
    x = 0
    mod = 1
    for b, m in zip(B, M):
        d, p, _ = extgcd(mod, m)
        if (b - x) % d != 0:
            return 0, -1
        x += (b - x) // d * p % (m // d) * mod
        mod *= m // d
    return x % mod, mod


if __name__ == "__main__":
    bm = tuple(tuple(map(int, input().split())) for _ in range(3))
    b, m = zip(*bm)
    x, mod = crt(b, m)
    if mod == -1:
        x = -1
    print(x)
0