結果

問題 No.186 中華風 (Easy)
ユーザー zundamo-tizundamo-ti
提出日時 2021-02-28 23:50:34
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 33 ms / 2,000 ms
コード長 884 bytes
コンパイル時間 879 ms
コンパイル使用メモリ 12,800 KB
実行使用メモリ 10,880 KB
最終ジャッジ日時 2024-04-14 03:03:28
合計ジャッジ時間 2,409 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

import sys
from math import gcd
readline = sys.stdin.readline

def lcm(a, b):
    return a * b // gcd(a, b)

def extgcd(a, b):
    assert(a >= b)
    if b == 0:
        return 1, 0
    x, y = extgcd(b, a % b)
    return y, x - (a // b) * y

def crt(r, m):
    r0, m0 = 0, 1
    for r1, m1 in zip(r, m):
        if m0 < m1:
            r0, r1 = r1, r0
            m0, m1 = m1, m0
        d = gcd(m0, m1)
        if (r1 - r0) % d != 0:
            return 0, 0
        l = lcm(m0, m1)
        x0, _ = extgcd(m0, m1)
        r0 = r0 + m0 * x0 * (r1 - r0) // d
        r0 %= l
        m0 = l
    return r0, m0

def main():
    r, m = [0] * 3, [0] * 3
    for i in range(3):
        r[i], m[i] = map(int, readline().split())
    ans, lcm = crt(r, m)
    if lcm == 0:
        print(-1)
        return
    if ans == 0:
        ans += lcm
    print(ans)

if __name__ == "__main__":
    main()
0