結果

問題 No.187 中華風 (Hard)
ユーザー rpy3cpprpy3cpp
提出日時 2015-09-18 20:13:08
言語 PyPy3
(7.3.13)
結果
WA  
実行時間 -
コード長 819 bytes
コンパイル時間 434 ms
コンパイル使用メモリ 87,040 KB
実行使用メモリ 76,996 KB
最終ジャッジ日時 2023-09-26 12:26:39
合計ジャッジ時間 7,125 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 AC 122 ms
76,704 KB
testcase_03 AC 124 ms
76,676 KB
testcase_04 AC 396 ms
76,820 KB
testcase_05 AC 392 ms
76,808 KB
testcase_06 AC 393 ms
76,784 KB
testcase_07 AC 391 ms
76,688 KB
testcase_08 AC 422 ms
76,780 KB
testcase_09 AC 423 ms
76,780 KB
testcase_10 AC 420 ms
76,996 KB
testcase_11 AC 395 ms
76,960 KB
testcase_12 AC 394 ms
76,824 KB
testcase_13 AC 89 ms
75,564 KB
testcase_14 AC 91 ms
75,448 KB
testcase_15 AC 119 ms
76,588 KB
testcase_16 AC 119 ms
76,632 KB
testcase_17 AC 73 ms
71,188 KB
testcase_18 WA -
testcase_19 AC 74 ms
71,188 KB
testcase_20 AC 301 ms
76,696 KB
testcase_21 AC 74 ms
71,288 KB
testcase_22 AC 395 ms
76,700 KB
testcase_23 AC 74 ms
71,284 KB
testcase_24 AC 73 ms
71,504 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
    for x1, y1 in xy[1:]:
        x0, y0 = solve2(x0, y0, x1, y1)
        if x0 == -1:
            return -1
    return x0 if x0 else y0

N, xy = read_data()
print(solve(N, xy) % (10**9 + 7))
0