結果

問題 No.187 中華風 (Hard)
ユーザー neterukunneterukun
提出日時 2021-05-03 14:03:19
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 489 ms / 3,000 ms
コード長 1,583 bytes
コンパイル時間 415 ms
コンパイル使用メモリ 86,928 KB
実行使用メモリ 78,712 KB
最終ジャッジ日時 2023-09-29 09:44:26
合計ジャッジ時間 9,477 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 95 ms
76,084 KB
testcase_01 AC 96 ms
76,136 KB
testcase_02 AC 443 ms
77,732 KB
testcase_03 AC 426 ms
77,576 KB
testcase_04 AC 489 ms
77,444 KB
testcase_05 AC 479 ms
77,524 KB
testcase_06 AC 483 ms
77,744 KB
testcase_07 AC 478 ms
77,752 KB
testcase_08 AC 419 ms
78,444 KB
testcase_09 AC 415 ms
78,712 KB
testcase_10 AC 418 ms
78,560 KB
testcase_11 AC 481 ms
77,772 KB
testcase_12 AC 483 ms
77,704 KB
testcase_13 AC 317 ms
77,320 KB
testcase_14 AC 316 ms
77,500 KB
testcase_15 AC 391 ms
77,496 KB
testcase_16 AC 392 ms
77,556 KB
testcase_17 AC 77 ms
71,360 KB
testcase_18 AC 93 ms
75,720 KB
testcase_19 AC 77 ms
71,060 KB
testcase_20 AC 392 ms
77,672 KB
testcase_21 AC 77 ms
71,172 KB
testcase_22 AC 483 ms
77,784 KB
testcase_23 AC 77 ms
71,228 KB
testcase_24 AC 76 ms
71,340 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def extended_gcd(a, b):
    if b == 0:
        return a, 1, 0
    else:
        g, y, x = extended_gcd(b, a % b)
        y -= (a // b) * x
        return g, x, y


def mod_inv(a, p):
    _, x, _ = extended_gcd(a, p)
    return x % p


def pregarner(a, m, MOD):
    def gcd(a, b):
        while b:
            a, b = b, a % b
        return a
    a, m = a[:], m[:]
    n = len(a)
    for i in range(n):
        for j in range(i):
            g = gcd(m[i], m[j])
            if (a[i] - a[j]) % g != 0:
                  return -1, a, m
            m[i], m[j] = m[i] // g, m[j] // g
            gi = gcd(m[i], g)
            gj = g // gi
            while True:
                g = gcd(gi, gj)
                gi, gj = gi * g, gj // g
                if g == 1:
                    break
            m[i], m[j] = m[i] * gi, m[j] * gj
            a[i], a[j] = a[i] % m[i], a[j] % m[j]
    lcm = 1
    for i in range(n):
        lcm = lcm * m[i] % MOD
    return lcm, a, m


def garner(a, m, MOD):
    n = len(m)
    m = m + [MOD]
    coeff = [1] * (n + 1)
    res = [0] * (n + 1)
    for i in range(n):
        t = (a[i] - res[i]) * mod_inv(coeff[i], m[i]) % m[i]
        for j in range(i + 1, n + 1):
            res[j] = (res[j] + t * coeff[j]) % m[j]
            coeff[j] = coeff[j] * m[i] % m[j]
    return res[-1]


n = int(input())
info = [list(map(int, input().split())) for i in range(n)]
MOD = 10 ** 9 + 7


x, y = list(zip(*info))
x, y = list(x), list(y)

lcm, x, y = pregarner(x, y, MOD)
if sum(x) == 0 or lcm == -1:
    print(lcm)
else:
    m = garner(x, y, MOD)
    print(m)
0