結果

問題 No.187 中華風 (Hard)
ユーザー rpy3cpprpy3cpp
提出日時 2015-09-18 20:29:48
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 397 ms / 3,000 ms
コード長 770 bytes
コンパイル時間 291 ms
コンパイル使用メモリ 87,212 KB
実行使用メモリ 76,848 KB
最終ジャッジ日時 2023-09-26 12:27:03
合計ジャッジ時間 6,682 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 84 ms
75,584 KB
testcase_01 AC 84 ms
75,360 KB
testcase_02 AC 111 ms
76,724 KB
testcase_03 AC 109 ms
76,532 KB
testcase_04 AC 368 ms
76,652 KB
testcase_05 AC 372 ms
76,784 KB
testcase_06 AC 367 ms
76,672 KB
testcase_07 AC 367 ms
76,760 KB
testcase_08 AC 397 ms
76,720 KB
testcase_09 AC 396 ms
76,792 KB
testcase_10 AC 397 ms
76,648 KB
testcase_11 AC 373 ms
76,504 KB
testcase_12 AC 372 ms
76,580 KB
testcase_13 AC 85 ms
75,456 KB
testcase_14 AC 85 ms
75,540 KB
testcase_15 AC 106 ms
76,640 KB
testcase_16 AC 105 ms
76,204 KB
testcase_17 AC 69 ms
71,352 KB
testcase_18 AC 82 ms
75,556 KB
testcase_19 AC 69 ms
71,460 KB
testcase_20 AC 282 ms
76,632 KB
testcase_21 AC 70 ms
71,480 KB
testcase_22 AC 368 ms
76,848 KB
testcase_23 AC 72 ms
71,608 KB
testcase_24 AC 71 ms
71,164 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 solve2(x1, y1, x2, y2):
    gcd = y1
    n = y2
    a = 1
    u = 0
    while n:
        k, gcd = divmod(gcd, n)
        gcd, n = n, gcd
        a, u = u, a - u * k
    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