結果

問題 No.187 中華風 (Hard)
ユーザー orisanoorisano
提出日時 2016-05-28 14:21:34
言語 Python2
(2.7.18)
結果
AC  
実行時間 1,002 ms / 3,000 ms
コード長 919 bytes
コンパイル時間 921 ms
コンパイル使用メモリ 6,604 KB
実行使用メモリ 8,124 KB
最終ジャッジ日時 2023-07-29 11:47:50
合計ジャッジ時間 14,082 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 480 ms
7,960 KB
testcase_01 AC 472 ms
7,948 KB
testcase_02 AC 125 ms
7,952 KB
testcase_03 AC 122 ms
8,048 KB
testcase_04 AC 920 ms
7,972 KB
testcase_05 AC 918 ms
8,124 KB
testcase_06 AC 922 ms
8,092 KB
testcase_07 AC 921 ms
8,092 KB
testcase_08 AC 1,002 ms
7,964 KB
testcase_09 AC 1,001 ms
7,956 KB
testcase_10 AC 1,002 ms
7,968 KB
testcase_11 AC 925 ms
7,980 KB
testcase_12 AC 924 ms
7,980 KB
testcase_13 AC 31 ms
7,800 KB
testcase_14 AC 31 ms
7,948 KB
testcase_15 AC 125 ms
7,900 KB
testcase_16 AC 129 ms
7,996 KB
testcase_17 AC 12 ms
5,868 KB
testcase_18 AC 328 ms
8,076 KB
testcase_19 AC 12 ms
6,048 KB
testcase_20 AC 646 ms
7,932 KB
testcase_21 AC 23 ms
7,784 KB
testcase_22 AC 919 ms
7,968 KB
testcase_23 AC 23 ms
7,648 KB
testcase_24 AC 23 ms
7,628 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from functools import *

def egcd(a, b):
    x, y, u, v = 0, 1, 1, 0
    while a != 0:
        q, r = b // a, b % a
        m, n = x - u * q, y - v * q
        b, a, x, y, u, v = a, r, u, v, m, n
    gcd = b
    return gcd, x, y

def lcm(a, b):
    from fractions import gcd
    return a // gcd(a, b) * b

def gcrt(congs):
    def fn(cong, prod):
        a, n = cong
        m = prod // n
        g, x, _ = egcd(m, n)
        return m // g * x * a

    def crt(a, b):
        N = a[1] * b[1]
        L = lcm(a[1], b[1])
        return (fn(a, N) + fn(b, N)) % L, L

    return reduce(crt, congs)

def main():
    N = int(raw_input())
    congs = [tuple([int(x) for x in raw_input().split(" ")]) for _ in range(N)]
    ans, mod = gcrt(congs)
    if ans == 0:
        ans = mod
    if any(ans % m != a for a, m in congs):
        print(-1)
    else:
        print(ans % (10**9 + 7))

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