結果

問題 No.187 中華風 (Hard)
ユーザー okayu29okayu29
提出日時 2023-01-25 22:46:30
言語 PyPy3
(7.3.15)
結果
RE  
実行時間 -
コード長 1,156 bytes
コンパイル時間 306 ms
コンパイル使用メモリ 87,236 KB
実行使用メモリ 83,432 KB
最終ジャッジ日時 2023-09-09 07:11:32
合計ジャッジ時間 10,286 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 89 ms
76,192 KB
testcase_01 AC 88 ms
75,956 KB
testcase_02 AC 455 ms
78,972 KB
testcase_03 AC 451 ms
78,908 KB
testcase_04 AC 538 ms
79,048 KB
testcase_05 AC 530 ms
78,124 KB
testcase_06 AC 518 ms
77,512 KB
testcase_07 AC 528 ms
77,924 KB
testcase_08 AC 519 ms
79,720 KB
testcase_09 AC 520 ms
79,540 KB
testcase_10 AC 519 ms
79,396 KB
testcase_11 AC 525 ms
78,264 KB
testcase_12 AC 521 ms
77,788 KB
testcase_13 AC 249 ms
76,932 KB
testcase_14 AC 248 ms
76,812 KB
testcase_15 RE -
testcase_16 RE -
testcase_17 AC 77 ms
71,344 KB
testcase_18 AC 90 ms
75,920 KB
testcase_19 AC 76 ms
71,388 KB
testcase_20 AC 428 ms
77,700 KB
testcase_21 AC 74 ms
71,380 KB
testcase_22 AC 530 ms
77,932 KB
testcase_23 AC 76 ms
71,456 KB
testcase_24 AC 74 ms
71,484 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

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)
    else:
        print(x%(10**9+7))
0