結果

問題 No.187 中華風 (Hard)
ユーザー Chihaya_chanChihaya_chan
提出日時 2020-08-10 21:52:25
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 477 ms / 3,000 ms
コード長 1,469 bytes
コンパイル時間 196 ms
コンパイル使用メモリ 82,176 KB
実行使用メモリ 77,696 KB
最終ジャッジ日時 2024-04-17 05:25:58
合計ジャッジ時間 6,719 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 55 ms
63,104 KB
testcase_01 AC 54 ms
63,616 KB
testcase_02 AC 106 ms
75,460 KB
testcase_03 AC 108 ms
75,720 KB
testcase_04 AC 448 ms
77,056 KB
testcase_05 AC 444 ms
76,800 KB
testcase_06 AC 448 ms
77,312 KB
testcase_07 AC 439 ms
77,184 KB
testcase_08 AC 463 ms
77,184 KB
testcase_09 AC 477 ms
77,460 KB
testcase_10 AC 471 ms
77,696 KB
testcase_11 AC 441 ms
76,544 KB
testcase_12 AC 440 ms
76,928 KB
testcase_13 AC 59 ms
66,304 KB
testcase_14 AC 59 ms
66,176 KB
testcase_15 AC 102 ms
75,520 KB
testcase_16 AC 102 ms
75,776 KB
testcase_17 AC 39 ms
52,864 KB
testcase_18 AC 53 ms
62,336 KB
testcase_19 AC 38 ms
52,736 KB
testcase_20 AC 332 ms
76,416 KB
testcase_21 AC 38 ms
52,352 KB
testcase_22 AC 443 ms
77,312 KB
testcase_23 AC 39 ms
52,224 KB
testcase_24 AC 38 ms
52,224 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#  中華風(easy)
import math


def xgcd(a, b):
    x0, y0, x1, y1 = 1, 0, 0, 1
    while b != 0:
        q, a, b = a // b, b, a % b
        x0, x1 = x1, x0 - q * x1
        y0, y1 = y1, y0 - q * y1
    return a, x0, y0

# すなわちax==1(mod m)なる自然数xを返してくれる関数 動作時間:logM


def modinv(a, m):
    g, x, y = xgcd(a, m)
    if g != 1:
        return False
    else:
        return x % m


def modular_numbers(pair1, pair2):
    x1, y1 = pair1
    x2, y2 = pair2
    g = math.gcd(y1, y2)
    if (x2-x1) % g != 0:
        return (float("inf"), float("inf"))
    else:
        K = (x2-x1)//g
        y1, y2 = y1//g, y2//g
        t = -K*modinv(y2, y1)
        m = x2+t*g*y2
        return (m % (g*y1*y2), g*y1*y2)


mod = 10**9 + 7


def main():
    N = int(input())
    que = [tuple(map(int, input().split())) for i in range(N)]
    if N == 1:
        print(que[0][0])
        return
    base = modular_numbers(que[0], que[1])

    if base[0] == float("inf"):
        print(-1)
        return
    for i in range(2, N):
        base = modular_numbers(base, que[i])
        if base[0] == float("inf"):
            print(-1)
            return
        else:
            continue
    if base[0] == float("inf"):
        print(-1)
        return
    else:
        if base[0] > 0:
            print(base[0] % mod)
            return
        else:
            print(base[1] % mod)
            return


if __name__ == "__main__":
    main()
0