結果

問題 No.187 中華風 (Hard)
ユーザー neterukunneterukun
提出日時 2021-05-03 13:48:41
言語 PyPy3
(7.3.13)
結果
WA  
実行時間 -
コード長 1,537 bytes
コンパイル時間 726 ms
コンパイル使用メモリ 86,996 KB
実行使用メモリ 78,944 KB
最終ジャッジ日時 2023-09-29 09:28:54
合計ジャッジ時間 9,850 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 87 ms
76,280 KB
testcase_01 AC 89 ms
75,992 KB
testcase_02 AC 443 ms
77,944 KB
testcase_03 AC 416 ms
77,768 KB
testcase_04 AC 481 ms
77,800 KB
testcase_05 AC 473 ms
77,864 KB
testcase_06 AC 473 ms
77,844 KB
testcase_07 AC 476 ms
77,860 KB
testcase_08 AC 413 ms
78,880 KB
testcase_09 AC 412 ms
78,944 KB
testcase_10 AC 411 ms
78,900 KB
testcase_11 AC 478 ms
77,784 KB
testcase_12 AC 474 ms
77,788 KB
testcase_13 AC 308 ms
77,564 KB
testcase_14 AC 309 ms
77,532 KB
testcase_15 WA -
testcase_16 WA -
testcase_17 AC 74 ms
71,324 KB
testcase_18 AC 87 ms
75,796 KB
testcase_19 AC 75 ms
71,268 KB
testcase_20 AC 393 ms
77,612 KB
testcase_21 AC 73 ms
71,268 KB
testcase_22 AC 474 ms
77,928 KB
testcase_23 AC 74 ms
71,072 KB
testcase_24 AC 75 ms
71,360 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(b, m, MOD):
    def gcd(a, b):
        while b:
            a, b = b, a % b
        return a

    res = 1
    n = len(b)
    for i in range(n):
        for j in range(i):
            g = gcd(m[i], m[j])
            if (b[i] - b[j]) % g != 0:
                  return -1
            m[i] //= g
            m[j] //= g
            gi = gcd(m[i], g)
            gj = g // gi

            while True:
                g = gcd(gi, gj)
                gi *= g
                gj //= g
                if g == 1:
                    break
            m[i] *= gi
            m[j] *= gj
            b[i] %= m[i]
            b[j] %= m[j]
    for i in range(n):
        res = res * m[i] % MOD
    return res


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 = pregarner(x, y, MOD)
if lcm == -1:
    print(lcm)
else:
    m = garner(x, y, MOD)
    print(m)
0