結果

問題 No.187 中華風 (Hard)
ユーザー nisizawa
提出日時 2017-12-17 11:44:05
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 822 ms / 3,000 ms
コード長 815 bytes
コンパイル時間 82 ms
コンパイル使用メモリ 12,672 KB
実行使用メモリ 16,000 KB
最終ジャッジ日時 2024-12-15 07:22:44
合計ジャッジ時間 10,628 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 25
権限があれば一括ダウンロードができます

ソースコード

diff #

from functools import lru_cache

@lru_cache(maxsize=None)
def ex_euclid(x, y):
    c0, c1 = x, y
    a0, a1 = 1, 0
    b0, b1 = 0, 1

    while c1 > 0:
        q, r = divmod(c0, c1)

        c0, c1 = c1, r
        a0, a1 = a1, (a0 - q * a1)
        b0, b1 = b1, (b0 - q * b1)

    return c0, a0, b0

def crt(x1, y1, x2, y2):
    gcd, a, b = ex_euclid(y1, y2)
    lcm = y1 * y2 // gcd
    
    c = x2 - x1
    if c % gcd != 0:
        return -1, lcm
    
    n = (y1 * c * a // gcd + x1) % lcm
    return n if n > 0 else lcm, lcm

def solve(xy_ls):
    x, y = xy_ls.pop()
    while len(xy_ls) > 0 and x >= 0:
        xt, yt = xy_ls.pop()
        x, y = crt(x, y, xt, yt)
    return x % (10**9 + 7) if x >= 0 else x

n = int(input())
xy_ls = [(tuple(map(int, input().split()))) for i in range(n)]

print(solve(xy_ls))
0