結果

問題 No.187 中華風 (Hard)
ユーザー neterukunneterukun
提出日時 2021-05-03 14:02:16
言語 PyPy3
(7.3.15)
結果
RE  
実行時間 -
コード長 1,573 bytes
コンパイル時間 326 ms
コンパイル使用メモリ 87,124 KB
実行使用メモリ 81,332 KB
最終ジャッジ日時 2023-09-29 09:43:16
合計ジャッジ時間 10,132 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 RE -
testcase_02 AC 441 ms
77,620 KB
testcase_03 AC 430 ms
77,456 KB
testcase_04 AC 491 ms
77,692 KB
testcase_05 AC 481 ms
77,652 KB
testcase_06 AC 485 ms
77,508 KB
testcase_07 AC 476 ms
77,636 KB
testcase_08 AC 423 ms
78,460 KB
testcase_09 AC 423 ms
78,544 KB
testcase_10 AC 422 ms
78,728 KB
testcase_11 AC 488 ms
77,424 KB
testcase_12 AC 487 ms
77,584 KB
testcase_13 AC 321 ms
77,328 KB
testcase_14 AC 318 ms
77,188 KB
testcase_15 AC 438 ms
77,828 KB
testcase_16 AC 441 ms
77,732 KB
testcase_17 AC 78 ms
71,372 KB
testcase_18 RE -
testcase_19 AC 78 ms
71,236 KB
testcase_20 AC 391 ms
77,772 KB
testcase_21 AC 75 ms
71,108 KB
testcase_22 AC 474 ms
77,992 KB
testcase_23 AC 76 ms
71,208 KB
testcase_24 AC 77 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
            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)
m = garner(x, y, MOD)
if sum(x) == 0 or lcm == -1:
    print(lcm)
else:
    print(m)
0