結果

問題 No.187 中華風 (Hard)
ユーザー okayu29okayu29
提出日時 2023-01-23 23:54:43
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,287 bytes
コンパイル時間 701 ms
コンパイル使用メモリ 87,228 KB
実行使用メモリ 79,668 KB
最終ジャッジ日時 2023-09-08 00:22:17
合計ジャッジ時間 10,068 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 83 ms
76,292 KB
testcase_01 AC 81 ms
76,344 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 AC 253 ms
76,776 KB
testcase_14 AC 254 ms
76,956 KB
testcase_15 AC 438 ms
77,972 KB
testcase_16 AC 436 ms
77,760 KB
testcase_17 AC 67 ms
71,284 KB
testcase_18 AC 80 ms
75,940 KB
testcase_19 AC 66 ms
71,472 KB
testcase_20 WA -
testcase_21 AC 67 ms
71,308 KB
testcase_22 WA -
testcase_23 AC 66 ms
71,728 KB
testcase_24 AC 66 ms
71,376 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, md):
    x = 0
    mm = 1
    for i in range(len(b)):
        mmi = 1
        for j in range(i):
            mmi *= m[j]
            mmi %= m[i]
        _, inv, _ = extgcd(mmi, m[i])
        x += mm*(inv*(b[i] - x) % m[i])
        x %= md
        mm *= m[i]
        mm %= md
    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

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, 10**9+7)
    if sum(b) == 0:
        print(mm)
    else:
        print(x%(10**9+7))

0