結果

問題 No.187 中華風 (Hard)
ユーザー orisanoorisano
提出日時 2016-05-28 14:21:34
言語 Python2
(2.7.18)
結果
AC  
実行時間 927 ms / 3,000 ms
コード長 919 bytes
コンパイル時間 219 ms
コンパイル使用メモリ 6,912 KB
実行使用メモリ 8,444 KB
最終ジャッジ日時 2024-04-16 18:09:38
合計ジャッジ時間 12,658 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 443 ms
8,316 KB
testcase_01 AC 435 ms
8,312 KB
testcase_02 AC 121 ms
8,312 KB
testcase_03 AC 117 ms
8,444 KB
testcase_04 AC 850 ms
8,316 KB
testcase_05 AC 845 ms
8,312 KB
testcase_06 AC 848 ms
8,316 KB
testcase_07 AC 848 ms
8,312 KB
testcase_08 AC 925 ms
8,444 KB
testcase_09 AC 927 ms
8,316 KB
testcase_10 AC 924 ms
8,316 KB
testcase_11 AC 849 ms
8,444 KB
testcase_12 AC 848 ms
8,440 KB
testcase_13 AC 30 ms
8,188 KB
testcase_14 AC 30 ms
8,188 KB
testcase_15 AC 120 ms
8,160 KB
testcase_16 AC 123 ms
8,288 KB
testcase_17 AC 12 ms
6,272 KB
testcase_18 AC 300 ms
8,412 KB
testcase_19 AC 12 ms
6,272 KB
testcase_20 AC 595 ms
8,292 KB
testcase_21 AC 22 ms
7,916 KB
testcase_22 AC 850 ms
8,440 KB
testcase_23 AC 23 ms
8,048 KB
testcase_24 AC 23 ms
8,044 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