結果

問題 No.186 中華風 (Easy)
ユーザー neterukunneterukun
提出日時 2020-12-30 05:37:05
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 43 ms / 2,000 ms
コード長 947 bytes
コンパイル時間 455 ms
コンパイル使用メモリ 82,304 KB
実行使用メモリ 52,352 KB
最終ジャッジ日時 2024-04-16 01:57:13
合計ジャッジ時間 2,213 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
51,712 KB
testcase_01 AC 42 ms
51,840 KB
testcase_02 AC 42 ms
51,840 KB
testcase_03 AC 43 ms
51,840 KB
testcase_04 AC 41 ms
52,096 KB
testcase_05 AC 41 ms
52,224 KB
testcase_06 AC 41 ms
51,840 KB
testcase_07 AC 42 ms
52,096 KB
testcase_08 AC 41 ms
51,840 KB
testcase_09 AC 41 ms
52,224 KB
testcase_10 AC 42 ms
51,840 KB
testcase_11 AC 41 ms
51,840 KB
testcase_12 AC 42 ms
51,712 KB
testcase_13 AC 41 ms
51,712 KB
testcase_14 AC 42 ms
52,096 KB
testcase_15 AC 43 ms
52,096 KB
testcase_16 AC 42 ms
52,096 KB
testcase_17 AC 41 ms
52,096 KB
testcase_18 AC 42 ms
51,712 KB
testcase_19 AC 42 ms
51,840 KB
testcase_20 AC 42 ms
52,352 KB
testcase_21 AC 42 ms
52,096 KB
testcase_22 AC 42 ms
52,352 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def extended_gcd(a, b):
    """ax + by = gcd(a, b) の整数解 (x, y) を求める"""
    if b == 0:
        return a, 1, 0
    else:
        g, y, x = extended_gcd(b, a % b)
        y -= (a // b) * x
        return g, x, y


def chinese_remainder_theorem(a, m):
    """すべての i について x = a[i] (mod. m[i]) を満たす x を求める
    x が存在しない場合は (0, -1) の組、x が存在するときは x = rem (mod. M)
    を満たす (rem, M) の組を返す"""
    rem, M = 0, 1
    for i in range(len(a)):
        g, p, q = extended_gcd(M, m[i])
        if (a[i] - rem) % g != 0:
            return 0, -1
        tmp = (a[i] - rem) // g * p % (m[i] // g)
        rem += M * tmp
        M *= m[i] // g
    return rem % M, M


info = [list(map(int, input().split())) for i in range(3)]


x, y = list(zip(*info))
r, m = chinese_remainder_theorem(x, y)
if m == -1:
    print(-1)
elif r == 0:
    print(m)
else:
    print(r)
0