結果

問題 No.187 中華風 (Hard)
ユーザー okayu29okayu29
提出日時 2023-01-24 10:05:35
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 491 ms / 3,000 ms
コード長 1,329 bytes
コンパイル時間 634 ms
コンパイル使用メモリ 87,264 KB
実行使用メモリ 79,148 KB
最終ジャッジ日時 2023-09-08 06:53:52
合計ジャッジ時間 10,047 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 93 ms
76,120 KB
testcase_01 AC 91 ms
75,780 KB
testcase_02 AC 463 ms
78,448 KB
testcase_03 AC 446 ms
77,960 KB
testcase_04 AC 491 ms
79,116 KB
testcase_05 AC 480 ms
77,792 KB
testcase_06 AC 482 ms
77,820 KB
testcase_07 AC 481 ms
77,796 KB
testcase_08 AC 472 ms
79,064 KB
testcase_09 AC 465 ms
79,056 KB
testcase_10 AC 468 ms
79,148 KB
testcase_11 AC 475 ms
77,528 KB
testcase_12 AC 487 ms
77,968 KB
testcase_13 AC 266 ms
76,960 KB
testcase_14 AC 266 ms
76,824 KB
testcase_15 AC 462 ms
78,416 KB
testcase_16 AC 458 ms
78,284 KB
testcase_17 AC 70 ms
71,168 KB
testcase_18 AC 84 ms
75,892 KB
testcase_19 AC 74 ms
71,108 KB
testcase_20 AC 395 ms
78,104 KB
testcase_21 AC 71 ms
71,616 KB
testcase_22 AC 484 ms
77,844 KB
testcase_23 AC 73 ms
71,416 KB
testcase_24 AC 68 ms
71,512 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):
    m.append(md)
    x = [0]*len(m)
    mm = [1]*len(m)
    for i in range(len(b)):
        _, inv, _ = extgcd(mm[i], m[i])
        for j in range(i+1, len(m)):
            x[j] += mm[j]*((b[i] - x[i]) * inv % m[i])
            x[j] %= m[j]
            mm[j] *= m[i]
            mm[j] %= m[j]
    return x[-1], mm[-1]

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 all(bi==0 for bi in b):
        print(mm)
    else:
        print(x%(10**9+7))

0