結果

問題 No.187 中華風 (Hard)
ユーザー い
提出日時 2023-09-21 14:21:13
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 764 ms / 3,000 ms
コード長 2,828 bytes
コンパイル時間 464 ms
コンパイル使用メモリ 86,712 KB
実行使用メモリ 84,192 KB
最終ジャッジ日時 2023-09-21 14:21:26
合計ジャッジ時間 11,159 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 167 ms
80,424 KB
testcase_01 AC 168 ms
80,396 KB
testcase_02 AC 289 ms
82,856 KB
testcase_03 AC 304 ms
82,708 KB
testcase_04 AC 476 ms
82,364 KB
testcase_05 AC 461 ms
82,588 KB
testcase_06 AC 488 ms
82,236 KB
testcase_07 AC 479 ms
82,272 KB
testcase_08 AC 759 ms
82,108 KB
testcase_09 AC 764 ms
82,028 KB
testcase_10 AC 739 ms
82,092 KB
testcase_11 AC 488 ms
82,348 KB
testcase_12 AC 519 ms
84,192 KB
testcase_13 AC 587 ms
81,264 KB
testcase_14 AC 513 ms
82,456 KB
testcase_15 AC 241 ms
82,468 KB
testcase_16 AC 244 ms
82,256 KB
testcase_17 AC 177 ms
80,096 KB
testcase_18 AC 164 ms
80,484 KB
testcase_19 AC 163 ms
80,500 KB
testcase_20 AC 434 ms
82,724 KB
testcase_21 AC 162 ms
80,500 KB
testcase_22 AC 500 ms
82,780 KB
testcase_23 AC 160 ms
80,244 KB
testcase_24 AC 159 ms
80,536 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

V = sys.version_info
_39 = False
_310 = False
_311 = False
if V.major == 3:
    _39 = V.minor >= 9
    _310 = V.minor >= 10
    _311 = V.minor >= 11

if _39:
    li = list
    tup = tuple
    dic = dict
    st = set
    ty = type
else:
    from typing import (
        List,
        Tuple,
        Type,
        Dict,
        Set,
    )

    li = List
    tup = Tuple
    dic = Dict
    st = Set
    ty = Type

from sys import stdin

read = stdin.buffer.read
rl = stdin.buffer.readline
rb = lambda: rl().split()
rls = stdin.buffer.readlines


from typing import (
    TypeVar,
    Sequence as Seq,
    Iterable as Iter,
    Protocol as Proto,
    Generic as Gen,
)
from typing import Callable as Fn


from typing import Iterable


def prints(
    a: Iterable[object],
    sep: str = "\n",
) -> None:
    print(sep.join(map(str, a)))


# import numpy as np


def egcd(
    a: int,
    b: int,
) -> tup[int, int, int]:
    if not b:
        if a < 0:
            return -a, -1, 0
        return a, 1, 0
    q, r = divmod(a, b)
    g, s, t = egcd(b, r)
    return g, t, s - q * t


def crt_mod(
    m: Seq[int],
    r: Seq[int],
    mod: int,
) -> int:
    l = len(m)
    assert len(r) == l
    m = list(m) + [mod]
    p = [1] * (l + 1)
    x = [0] * (l + 1)
    for i, (n, b) in enumerate(zip(m, r)):
        assert n > 0
        g, ip, _ = egcd(p[i], n)
        assert g == 1
        t = (b - x[i]) % n * ip % n
        for j in range(i + 1, l + 1):
            x[j] += t * p[j]
            x[j] %= m[j]
            p[j] = p[j] * n % m[j]
    return x[-1]


def factorize(n: int) -> li[tup[int, int]]:
    f = []
    for i in range(2, n):
        if i * i > n:
            break
        if n % i:
            continue
        c = 0
        while n % i == 0:
            n //= i
            c += 1
        f.append((i, c))
    if n > 1:
        f.append((n, 1))
    return f


def to_coprime(mr: li[tup[int, int]]):
    f = dict()
    for m, r in mr:
        for p, e in factorize(m):
            n = pow(p, e)
            b = r % n
            l, a = f.get(p, (1, 0))
            if l < n:
                l, n = n, l
                a, b = b, a
            if a % n != b:
                mr.clear()
                return
            f[p] = (l, a)
    mr.clear()
    for x in f.values():
        mr.append(x)


def solve() -> None:
    n = int(rl())
    mr = []
    for _ in range(n):
        r, m = map(int, rb())
        mr.append((m, r))
    to_coprime(mr)
    n = len(mr)
    if n == 0:
        print(-1)
        return
    m, r = tuple(zip(*mr))
    mod = 10**9 + 7
    if max(r) > 0:
        x = crt_mod(m, r, mod)
        print(x)
        return
    l = 1
    for x in m:
        l = l * x % mod
    print(l)


def main() -> None:
    t = 1
    # t = int(rl())
    for _ in range(t):
        solve()


main()
0