結果

問題 No.187 中華風 (Hard)
ユーザー keitel339keitel339
提出日時 2021-03-03 00:32:32
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,610 bytes
コンパイル時間 182 ms
コンパイル使用メモリ 82,400 KB
実行使用メモリ 71,908 KB
最終ジャッジ日時 2024-04-14 05:45:21
合計ジャッジ時間 5,658 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 52 ms
63,116 KB
testcase_01 AC 53 ms
63,588 KB
testcase_02 AC 248 ms
71,908 KB
testcase_03 AC 250 ms
70,576 KB
testcase_04 AC 264 ms
70,964 KB
testcase_05 AC 266 ms
70,440 KB
testcase_06 AC 264 ms
70,576 KB
testcase_07 AC 264 ms
71,096 KB
testcase_08 AC 255 ms
71,296 KB
testcase_09 AC 256 ms
70,900 KB
testcase_10 AC 256 ms
71,116 KB
testcase_11 AC 265 ms
71,644 KB
testcase_12 AC 265 ms
71,764 KB
testcase_13 AC 158 ms
68,836 KB
testcase_14 AC 159 ms
68,888 KB
testcase_15 WA -
testcase_16 WA -
testcase_17 AC 39 ms
52,596 KB
testcase_18 AC 51 ms
61,896 KB
testcase_19 AC 38 ms
53,756 KB
testcase_20 AC 216 ms
70,384 KB
testcase_21 AC 38 ms
53,148 KB
testcase_22 AC 263 ms
71,184 KB
testcase_23 AC 39 ms
52,656 KB
testcase_24 AC 37 ms
53,272 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 と mの積を求める。
    多倍長整数を回避。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, _ = garner(x, y, mod)
print(ans)
0