結果

問題 No.187 中華風 (Hard)
ユーザー rpy3cpprpy3cpp
提出日時 2015-09-18 20:17:41
言語 PyPy3
(7.3.13)
結果
RE  
実行時間 -
コード長 832 bytes
コンパイル時間 305 ms
コンパイル使用メモリ 86,852 KB
実行使用メモリ 80,464 KB
最終ジャッジ日時 2023-09-26 12:26:49
合計ジャッジ時間 7,265 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 84 ms
75,044 KB
testcase_01 AC 84 ms
74,928 KB
testcase_02 AC 120 ms
76,008 KB
testcase_03 AC 118 ms
76,672 KB
testcase_04 AC 389 ms
76,660 KB
testcase_05 AC 393 ms
76,504 KB
testcase_06 AC 388 ms
76,444 KB
testcase_07 AC 389 ms
76,200 KB
testcase_08 AC 415 ms
76,548 KB
testcase_09 AC 417 ms
76,516 KB
testcase_10 AC 416 ms
76,176 KB
testcase_11 AC 394 ms
76,584 KB
testcase_12 AC 388 ms
76,820 KB
testcase_13 AC 86 ms
75,280 KB
testcase_14 AC 84 ms
74,928 KB
testcase_15 RE -
testcase_16 RE -
testcase_17 AC 72 ms
71,128 KB
testcase_18 AC 83 ms
75,544 KB
testcase_19 AC 72 ms
71,380 KB
testcase_20 AC 301 ms
76,380 KB
testcase_21 AC 74 ms
71,128 KB
testcase_22 AC 395 ms
76,548 KB
testcase_23 AC 72 ms
71,280 KB
testcase_24 AC 70 ms
70,880 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

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