結果

問題 No.187 中華風 (Hard)
ユーザー shinnshinnshinnshinn
提出日時 2022-03-02 23:18:01
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 780 ms / 3,000 ms
コード長 874 bytes
コンパイル時間 322 ms
コンパイル使用メモリ 10,872 KB
実行使用メモリ 8,644 KB
最終ジャッジ日時 2023-09-23 14:05:58
合計ジャッジ時間 9,841 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 18 ms
8,468 KB
testcase_01 AC 18 ms
8,512 KB
testcase_02 AC 52 ms
8,544 KB
testcase_03 AC 49 ms
8,644 KB
testcase_04 AC 700 ms
8,556 KB
testcase_05 AC 688 ms
8,596 KB
testcase_06 AC 690 ms
8,520 KB
testcase_07 AC 694 ms
8,636 KB
testcase_08 AC 780 ms
8,440 KB
testcase_09 AC 762 ms
8,584 KB
testcase_10 AC 764 ms
8,436 KB
testcase_11 AC 686 ms
8,552 KB
testcase_12 AC 691 ms
8,492 KB
testcase_13 AC 19 ms
8,332 KB
testcase_14 AC 18 ms
8,332 KB
testcase_15 AC 42 ms
8,528 KB
testcase_16 AC 45 ms
8,592 KB
testcase_17 AC 15 ms
7,784 KB
testcase_18 AC 17 ms
8,404 KB
testcase_19 AC 15 ms
7,924 KB
testcase_20 AC 485 ms
8,600 KB
testcase_21 AC 15 ms
7,832 KB
testcase_22 AC 694 ms
8,476 KB
testcase_23 AC 14 ms
7,832 KB
testcase_24 AC 14 ms
7,828 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def gcd(a, b):
    if b == 0:
        return a
    else:
        return gcd(b, a%b)

def extgcd(a, b):
    #拡張ユークリッド互除法
    #ax + by = gcd(a, b)となる(x, y)を求めます
    if b == 0:
        return [1, 0]

    q = a//b
    r = a%b
    
    s, t = extgcd(b, r)
    y = s - q*t
    return [t, y]

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

def CRT(rs, ms):
    R = 0
    M = 1
    for r, m in zip(rs, ms):
        g = gcd(M, m)
        s, t = extgcd(M, m)

        if r%g != R%g:
            print(-1)
            exit(0)

        R = (m*t*R + M*s*r) // g
        M = lcm(m, M)
        R %= M
    return [R, M]

x = []
y = []
v = []
n = int(input())
for i in range(n):
    a, b = map(int, input().split())
    x.append(a)
    y.append(b)
    v.append([a, b])

r, m = CRT(x, y)

MOD = 10**9 + 7
if r == 0:
    print(m%MOD)
else:
    print(r%MOD)
0