結果

問題 No.187 中華風 (Hard)
ユーザー okayu29okayu29
提出日時 2023-01-18 09:25:56
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 453 ms / 3,000 ms
コード長 793 bytes
コンパイル時間 1,496 ms
コンパイル使用メモリ 86,924 KB
実行使用メモリ 78,716 KB
最終ジャッジ日時 2023-09-02 05:30:42
合計ジャッジ時間 8,313 ms
ジャッジサーバーID
(参考情報)
judge12 / judge16
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 84 ms
75,404 KB
testcase_01 AC 84 ms
75,284 KB
testcase_02 AC 140 ms
77,152 KB
testcase_03 AC 148 ms
78,684 KB
testcase_04 AC 424 ms
78,044 KB
testcase_05 AC 420 ms
78,400 KB
testcase_06 AC 417 ms
77,364 KB
testcase_07 AC 423 ms
78,208 KB
testcase_08 AC 450 ms
78,420 KB
testcase_09 AC 453 ms
78,440 KB
testcase_10 AC 453 ms
78,400 KB
testcase_11 AC 416 ms
77,636 KB
testcase_12 AC 419 ms
77,316 KB
testcase_13 AC 89 ms
75,824 KB
testcase_14 AC 89 ms
75,996 KB
testcase_15 AC 156 ms
78,716 KB
testcase_16 AC 138 ms
76,892 KB
testcase_17 AC 74 ms
71,496 KB
testcase_18 AC 84 ms
75,480 KB
testcase_19 AC 74 ms
71,356 KB
testcase_20 AC 319 ms
77,428 KB
testcase_21 AC 74 ms
71,320 KB
testcase_22 AC 415 ms
77,132 KB
testcase_23 AC 73 ms
71,644 KB
testcase_24 AC 72 ms
71,172 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from math import gcd

def extgcd(a, b, x1=1, y1=0, x2=0, y2=1):
    if b == 0:
        return a, x1, y1
    q = a//b
    return extgcd(b, a%b, x2 ,y2, x1-x2*q, y1-y2*q)

def crt2(a, b, m1, m2):
    if a < b:
        a, b = b, a
        m1, m2 = m2, m1
    d, im1, im2 = extgcd(m1, m2)
    if a%d != b%d:
        return None
    mm = m1//d*m2
    x = (a - ((a-b)//d)*(m1*im1)) % mm
    return x, mm
def crt(as_, ms):
    x = as_[0]
    mm = ms[0]
    for a, m in zip(as_[1:], ms[1:]):
        t = crt2(a, x, m, mm)
        if t is None:
            return None
        x, mm = t
    return x, mm
n = int(input())
as_, ms = zip(*(map(int, input().split()) for _ in range(n)))

t = crt(as_, ms)
if t is None:
    print(-1)
else:
    x, mm = t
    if x == 0:
        x = mm
    print(x%(10**9+7))
0