結果

問題 No.187 中華風 (Hard)
ユーザー keitel339keitel339
提出日時 2021-03-03 00:24:25
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,617 bytes
コンパイル時間 412 ms
コンパイル使用メモリ 82,476 KB
実行使用メモリ 72,272 KB
最終ジャッジ日時 2024-04-14 05:44:52
合計ジャッジ時間 5,968 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 53 ms
63,548 KB
testcase_01 AC 52 ms
62,524 KB
testcase_02 AC 248 ms
71,352 KB
testcase_03 AC 246 ms
72,272 KB
testcase_04 AC 263 ms
71,284 KB
testcase_05 AC 264 ms
71,068 KB
testcase_06 AC 264 ms
71,920 KB
testcase_07 AC 265 ms
71,548 KB
testcase_08 AC 256 ms
70,684 KB
testcase_09 AC 254 ms
70,580 KB
testcase_10 AC 255 ms
72,268 KB
testcase_11 AC 263 ms
71,372 KB
testcase_12 AC 263 ms
71,004 KB
testcase_13 AC 160 ms
69,672 KB
testcase_14 AC 161 ms
69,948 KB
testcase_15 AC 250 ms
71,168 KB
testcase_16 AC 249 ms
72,020 KB
testcase_17 AC 39 ms
53,148 KB
testcase_18 AC 52 ms
62,564 KB
testcase_19 AC 38 ms
53,704 KB
testcase_20 AC 216 ms
71,440 KB
testcase_21 AC 39 ms
53,640 KB
testcase_22 AC 263 ms
70,620 KB
testcase_23 WA -
testcase_24 AC 38 ms
54,508 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def inverse(a, mod):
    '''
    a, mod が互いに素な場合modular逆数を返す
    '''
    assert mod > 0
    a %= mod
    p = mod
    x, y = 0, 1
    while a > 0:
        n = p // a
        p, a = a, p % a, 
        x, y = y, x - n * y
    return x % mod if p == 1 else -1

import math
def preprocess_garner(b, m, mod):
    n = len(m)
    assert len(b) == n
    for i in range(n):
        for j in range(i+1, n):
            g = math.gcd(m[i], m[j])
            if (b[i] - b[j]) % g != 0:
                return False
            m[i] //= g
            m[j] //= g
            gi = math.gcd(m[i], g)
            gj = g // gi
            while True:
                g = math.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]
    return True

def garner(b, m, mod):
    '''
    互いに素なmに対してall(x≡b[i](mod, m[i]))を満たす x を求める。
    多倍長整数を回避。O(n^2+nlogn)
    '''
    n = len(m)
    s = [0] * (n+1)
    p = [1] * (n+1)
    m.append(mod)
    for i in range(n):
        t = (b[i] - s[i]) * inverse(p[i], m[i]) % m[i]
        for j in range(i+1, n+1):
            s[j] = (s[j] + t * p[j]) % m[j]
            p[j] = p[j] * m[i] % m[j]
    m.pop()
    return s[-1], p[-1]

n = int(input())
x, y = [0] * n, [0] * n
for i in range(n):
    x[i], y[i] = map(int, input().split())
mod = 10**9+7
if not preprocess_garner(x, y, mod):
    print(-1)
    quit()
ans, lcm = garner(x, y, mod)
print(ans if ans else lcm)
0