結果

問題 No.186 中華風 (Easy)
ユーザー maspy
提出日時 2020-03-03 21:13:20
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 29 ms / 2,000 ms
コード長 936 bytes
コンパイル時間 122 ms
コンパイル使用メモリ 12,544 KB
実行使用メモリ 10,624 KB
最終ジャッジ日時 2024-07-19 18:38:23
合計ジャッジ時間 1,336 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 23
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/usr/bin/env python3
# %%
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines


# %%
m = map(int, read().split())
XY = zip(m, m)
X, Y = zip(*XY)

# %%


class NotFoundError(Exception):
    pass


def extgcd(a, b):
    """return x,y,d such that ax + by = d > 0"""
    x, y, u, v, s, t = 1, 0, 0, 1, a, b
    while t:
        k = s // t
        s, t = t, s - k * t
        x, u = u, x - k * u
        y, v = v, y - k * v
    return x, y, s


def CRT(a, n, b, m):
    """find (c, mod) where x = a (n), x = b (m) iff x = c (mod)"""
    p, q, d = extgcd(n, m)
    if (a - b) % d != 0:
        raise NotFoundError
    mod = n * (m // d)
    a += n * ((b - a) // d * p % (m // d))
    return a, mod


# %%
x, n = 0, 1
for y, m in zip(X, Y):
    try:
        x, n = CRT(x, n, y, m)
    except NotFoundError:
        print(-1)
        exit()

x %= n
if not x:
    x = n
print(x)
0