結果

問題 No.187 中華風 (Hard)
ユーザー nisizawanisizawa
提出日時 2017-12-17 11:44:05
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 836 ms / 3,000 ms
コード長 815 bytes
コンパイル時間 640 ms
コンパイル使用メモリ 10,848 KB
実行使用メモリ 14,104 KB
最終ジャッジ日時 2023-08-21 17:48:21
合計ジャッジ時間 11,711 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 23 ms
8,868 KB
testcase_01 AC 22 ms
8,748 KB
testcase_02 AC 63 ms
12,036 KB
testcase_03 AC 62 ms
11,900 KB
testcase_04 AC 766 ms
13,756 KB
testcase_05 AC 760 ms
13,876 KB
testcase_06 AC 766 ms
13,596 KB
testcase_07 AC 767 ms
13,736 KB
testcase_08 AC 834 ms
14,104 KB
testcase_09 AC 833 ms
14,028 KB
testcase_10 AC 836 ms
13,908 KB
testcase_11 AC 762 ms
13,676 KB
testcase_12 AC 763 ms
13,808 KB
testcase_13 AC 24 ms
8,932 KB
testcase_14 AC 24 ms
8,832 KB
testcase_15 AC 398 ms
11,788 KB
testcase_16 AC 413 ms
11,908 KB
testcase_17 AC 20 ms
8,580 KB
testcase_18 AC 23 ms
8,864 KB
testcase_19 AC 20 ms
8,796 KB
testcase_20 AC 530 ms
12,472 KB
testcase_21 AC 20 ms
8,764 KB
testcase_22 AC 762 ms
13,844 KB
testcase_23 AC 19 ms
8,620 KB
testcase_24 AC 19 ms
8,796 KB
権限があれば一括ダウンロードができます

ソースコード

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