結果

問題 No.187 中華風 (Hard)
ユーザー い
提出日時 2023-09-21 02:09:12
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 874 ms / 3,000 ms
コード長 2,615 bytes
コンパイル時間 92 ms
コンパイル使用メモリ 13,184 KB
実行使用メモリ 44,628 KB
最終ジャッジ日時 2024-07-06 20:53:25
合計ジャッジ時間 15,590 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 451 ms
44,116 KB
testcase_01 AC 451 ms
44,492 KB
testcase_02 AC 490 ms
44,500 KB
testcase_03 AC 492 ms
44,248 KB
testcase_04 AC 481 ms
44,484 KB
testcase_05 AC 485 ms
43,988 KB
testcase_06 AC 494 ms
43,988 KB
testcase_07 AC 483 ms
44,104 KB
testcase_08 AC 482 ms
44,104 KB
testcase_09 AC 481 ms
43,988 KB
testcase_10 AC 479 ms
44,628 KB
testcase_11 AC 487 ms
44,484 KB
testcase_12 AC 484 ms
44,104 KB
testcase_13 AC 483 ms
44,112 KB
testcase_14 AC 475 ms
44,500 KB
testcase_15 AC 832 ms
44,480 KB
testcase_16 AC 874 ms
43,984 KB
testcase_17 AC 445 ms
44,616 KB
testcase_18 AC 450 ms
44,504 KB
testcase_19 AC 447 ms
44,104 KB
testcase_20 AC 474 ms
44,104 KB
testcase_21 AC 442 ms
43,992 KB
testcase_22 AC 484 ms
44,244 KB
testcase_23 AC 442 ms
44,104 KB
testcase_24 AC 446 ms
44,232 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: np.ndarray,
    r: np.ndarray,
    mod: int,
) -> int:
    l = len(m)
    assert len(r) == l
    m = np.append(m, mod)
    p = np.ones(l + 1, np.int64)
    x = np.zeros(l + 1, np.int64)
    for i in range(l):
        n = m[i]
        assert n > 0
        g, ip, _ = egcd(p[i], n)
        q, a = divmod(r[i] - x[i], g)
        if a:
            return -1
        n //= g
        t = q % n * ip % n
        x += t * p
        x %= m
        p *= n
        p %= m
    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 factorize_lcm(
    a: Iter[int],
) -> dic[int, int]:
    f = dict()
    for x in a:
        for p, e in factorize(x):
            c = f.get(p, 0)
            f[p] = max(c, e)
    return f


def lcm_mod(m: int, a: Iter[int]) -> int:
    l = 1
    for p, e in factorize_lcm(a).items():
        l = l * pow(p, e, m) % m
    return l


def solve() -> None:
    n = int(rl())
    rm = [map(int, rb()) for _ in range(n)]
    r, m = list(zip(*rm))
    mod = 10**9 + 7
    if max(r) == 0:
        print(lcm_mod(mod, m))
    else:
        m = np.array(m)
        r = np.array(r)
        print(crt_mod(m, r, mod))


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


main()
0