結果

問題 No.187 中華風 (Hard)
ユーザー okayu29okayu29
提出日時 2023-01-26 13:30:56
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 483 ms / 3,000 ms
コード長 1,483 bytes
コンパイル時間 261 ms
コンパイル使用メモリ 87,176 KB
実行使用メモリ 79,664 KB
最終ジャッジ日時 2023-09-09 15:29:04
合計ジャッジ時間 9,429 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 79 ms
76,044 KB
testcase_01 AC 80 ms
76,004 KB
testcase_02 AC 453 ms
77,792 KB
testcase_03 AC 427 ms
78,328 KB
testcase_04 AC 481 ms
79,036 KB
testcase_05 AC 482 ms
79,224 KB
testcase_06 AC 482 ms
79,348 KB
testcase_07 AC 464 ms
77,868 KB
testcase_08 AC 452 ms
77,964 KB
testcase_09 AC 453 ms
77,532 KB
testcase_10 AC 448 ms
77,696 KB
testcase_11 AC 467 ms
79,664 KB
testcase_12 AC 483 ms
79,100 KB
testcase_13 AC 257 ms
76,636 KB
testcase_14 AC 251 ms
76,600 KB
testcase_15 AC 428 ms
77,476 KB
testcase_16 AC 444 ms
77,496 KB
testcase_17 AC 69 ms
71,252 KB
testcase_18 AC 82 ms
75,940 KB
testcase_19 AC 65 ms
71,372 KB
testcase_20 AC 375 ms
78,084 KB
testcase_21 AC 66 ms
71,216 KB
testcase_22 AC 463 ms
77,920 KB
testcase_23 AC 66 ms
71,132 KB
testcase_24 AC 66 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):
    x = [0]*(len(b)+1)
    for i in range(len(b)):
        v = 0
        mm = rmm = 1
        for j in range(i):
            v += (mm * x[j+1]) % m[i]
            v %= m[i]
            mm *= m[j]
            mm %= m[i]
        rmm = extgcd(mm, m[i])[1]
        rmm %= m[i]
        x[i+1] = rmm * (b[i] - v)
        x[i+1] %= m[i]
    xx = 0
    mm = 1
    for i in range(len(b)):
        xx += mm*x[i+1]
        xx %= md
        mm *= m[i]
        mm %= md
    return xx, 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, md)
    if all(bi==0 for bi in b):
        print(mm)
    else:
        print(x)
0