結果

問題 No.187 中華風 (Hard)
ユーザー okayu29okayu29
提出日時 2023-01-25 22:49:28
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 535 ms / 3,000 ms
コード長 1,164 bytes
コンパイル時間 379 ms
コンパイル使用メモリ 87,280 KB
実行使用メモリ 79,604 KB
最終ジャッジ日時 2023-09-09 07:12:28
合計ジャッジ時間 10,201 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 88 ms
76,272 KB
testcase_01 AC 87 ms
75,856 KB
testcase_02 AC 458 ms
78,712 KB
testcase_03 AC 451 ms
78,872 KB
testcase_04 AC 535 ms
79,088 KB
testcase_05 AC 526 ms
78,024 KB
testcase_06 AC 519 ms
77,828 KB
testcase_07 AC 527 ms
78,204 KB
testcase_08 AC 516 ms
79,604 KB
testcase_09 AC 517 ms
79,236 KB
testcase_10 AC 517 ms
79,304 KB
testcase_11 AC 522 ms
77,916 KB
testcase_12 AC 517 ms
77,696 KB
testcase_13 AC 246 ms
76,708 KB
testcase_14 AC 244 ms
76,884 KB
testcase_15 AC 453 ms
78,488 KB
testcase_16 AC 468 ms
78,384 KB
testcase_17 AC 72 ms
71,320 KB
testcase_18 AC 86 ms
76,140 KB
testcase_19 AC 72 ms
71,324 KB
testcase_20 AC 425 ms
77,812 KB
testcase_21 AC 72 ms
71,668 KB
testcase_22 AC 526 ms
77,804 KB
testcase_23 AC 73 ms
71,336 KB
testcase_24 AC 72 ms
71,480 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 garner(b, m):
    x = 0
    mm = 1
    for i in range(len(m)):
        _, imm, _ = extgcd(mm, m[i])
        x += mm*(imm*(b[i]-x)%m[i])
        mm *= m[i]
    return x, mm

def pre_garner(b, m):
    for i in range(len(m)):
        for j in range(len(m)):
            if i == j:
                continue
            g = gcd(m[i], m[j])
            if (b[i]-b[j])%g != 0:
                return False
            m[i] //= g
            m[j] //= g
            gi = gcd(g, m[i])
            gj = g//gi
            while g != 1:
                g = gcd(gi, gj)
                gi *= g
                gj //= g
            m[i] *= gi
            m[j] *= gj
            b[i] %= m[i]
            b[j] %= m[j]
    return True
md = 10**9+7
n = int(input())
b, m = zip(*(tuple(map(int, input().split())) for _ in range(n)))
b, m = list(b), list(m)
if not pre_garner(b, m):
    print(-1)
else:
    x, mm = garner(b, m)
    if all(bi==0 for bi in b):
        print(mm%md)
    else:
        print(x%md)
0