結果

問題 No.187 中華風 (Hard)
ユーザー rpy3cpprpy3cpp
提出日時 2015-09-18 20:20:46
言語 PyPy3
(7.3.13)
結果
AC  
実行時間 420 ms / 3,000 ms
コード長 838 bytes
コンパイル時間 275 ms
コンパイル使用メモリ 86,660 KB
実行使用メモリ 76,848 KB
最終ジャッジ日時 2023-09-26 12:26:56
合計ジャッジ時間 6,907 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 83 ms
75,316 KB
testcase_01 AC 85 ms
75,304 KB
testcase_02 AC 120 ms
76,324 KB
testcase_03 AC 118 ms
76,548 KB
testcase_04 AC 390 ms
76,640 KB
testcase_05 AC 390 ms
76,392 KB
testcase_06 AC 388 ms
76,624 KB
testcase_07 AC 390 ms
76,528 KB
testcase_08 AC 420 ms
76,668 KB
testcase_09 AC 418 ms
76,356 KB
testcase_10 AC 420 ms
76,680 KB
testcase_11 AC 390 ms
76,588 KB
testcase_12 AC 392 ms
76,536 KB
testcase_13 AC 85 ms
75,340 KB
testcase_14 AC 86 ms
75,252 KB
testcase_15 AC 114 ms
76,660 KB
testcase_16 AC 115 ms
76,052 KB
testcase_17 AC 71 ms
71,296 KB
testcase_18 AC 81 ms
75,320 KB
testcase_19 AC 70 ms
71,356 KB
testcase_20 AC 294 ms
76,732 KB
testcase_21 AC 70 ms
71,020 KB
testcase_22 AC 390 ms
76,848 KB
testcase_23 AC 68 ms
71,316 KB
testcase_24 AC 69 ms
71,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def read_data():
    N = int(input())
    xy = []
    for n in range(N):
        x, y = map(int, input().split())
        xy.append((x, y))
    return N, xy


def gcd_ext(m, n):
    x, y, u, v = 1, 0, 0, 1
    while n:
        k, m = divmod(m, n)
        m, n = n, m
        x, y, u, v = u, v, x - u * k, y - v * k
    return m, x, y


def solve2(x1, y1, x2, y2):
    gcd, a, b = gcd_ext(y1, y2)
    lcm = y1 * y2 // gcd
    k, r = divmod(x1 - x2, gcd)
    if r != 0:
        return -1, lcm
    z = (x1 - k * a * y1) % lcm
    return z, lcm


def solve(N, xy):
    x0, y0 = xy[0]
    if N == 1:
        return x0 if x0 else y0
    mod = 10 ** 9 + 7
    for x1, y1 in xy[1:]:
        x0, y0 = solve2(x0, y0, x1, y1)
        if x0 == -1:
            return -1
    return x0 % mod if x0 else y0 % mod

N, xy = read_data()
print(solve(N, xy))
0