結果

問題 No.187 中華風 (Hard)
ユーザー okayu29okayu29
提出日時 2023-01-18 09:24:52
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 783 bytes
コンパイル時間 351 ms
コンパイル使用メモリ 86,872 KB
実行使用メモリ 84,548 KB
最終ジャッジ日時 2023-09-02 05:29:52
合計ジャッジ時間 8,354 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 79 ms
75,544 KB
testcase_01 AC 79 ms
75,540 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 WA -
testcase_14 WA -
testcase_15 RE -
testcase_16 RE -
testcase_17 AC 65 ms
71,220 KB
testcase_18 AC 75 ms
75,640 KB
testcase_19 AC 69 ms
71,392 KB
testcase_20 RE -
testcase_21 WA -
testcase_22 RE -
testcase_23 WA -
testcase_24 WA -
権限があれば一括ダウンロードができます

ソースコード

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)
0