結果

問題 No.187 中華風 (Hard)
ユーザー hly1204hly1204
提出日時 2021-05-08 09:53:15
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 84 ms / 3,000 ms
コード長 905 bytes
コンパイル時間 87 ms
コンパイル使用メモリ 10,820 KB
実行使用メモリ 8,432 KB
最終ジャッジ日時 2023-10-14 12:54:09
合計ジャッジ時間 3,143 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
8,316 KB
testcase_01 AC 16 ms
8,188 KB
testcase_02 AC 42 ms
8,312 KB
testcase_03 AC 41 ms
8,260 KB
testcase_04 AC 78 ms
8,376 KB
testcase_05 AC 78 ms
8,432 KB
testcase_06 AC 78 ms
8,308 KB
testcase_07 AC 77 ms
8,432 KB
testcase_08 AC 84 ms
8,356 KB
testcase_09 AC 82 ms
8,348 KB
testcase_10 AC 81 ms
8,432 KB
testcase_11 AC 78 ms
8,428 KB
testcase_12 AC 78 ms
8,428 KB
testcase_13 AC 21 ms
8,284 KB
testcase_14 AC 21 ms
8,288 KB
testcase_15 AC 41 ms
8,360 KB
testcase_16 AC 42 ms
8,208 KB
testcase_17 AC 17 ms
7,788 KB
testcase_18 AC 17 ms
8,244 KB
testcase_19 AC 17 ms
7,748 KB
testcase_20 AC 65 ms
8,304 KB
testcase_21 AC 16 ms
7,808 KB
testcase_22 AC 78 ms
8,356 KB
testcase_23 AC 16 ms
7,800 KB
testcase_24 AC 17 ms
7,748 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

# -*- coding: UTF-8 -*-

def exgcd(a: int, b: int) -> (int, int, int):
    # 返回 (gcd(a,b), x, y) 满足 gcd(a,b)=ax+by
    x1, x2, x3, x4 = 1, 0, 0, 1
    while b != 0:
        q = a // b
        x1, x2, x3, x4, a, b = x3, x4, x1 - x3 * q, x2 - x4 * q, b, a - b * q
    return a, x1, x2


def gcrt2(a: int, m1: int, b: int, m2: int) -> (int, int):
    d, x, y = exgcd(m1, m2)
    bma = b - a
    if bma % d != 0:
        return -1, -1
    t, y = m2 // d, bma // d
    res = x * y % t
    if res < 0:
        res += t
    return res * m1 + a, m1 * t

if __name__ == '__main__':
    n = int(input())
    ans, m0 = 0, 1
    for _ in range(0, n):
        c, d = (int(i) for i in input().split(' '))
        ans, m0 = gcrt2(ans, m0, c, d)
        if ans == -1:
            break
    if ans == -1:
        print(-1)
    elif ans == 0:
        print(m0 % 1000000007)
    else:
        print(ans % 1000000007)
0