結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 445 ms
8,424 KB
testcase_01 AC 435 ms
8,424 KB
testcase_02 AC 120 ms
8,292 KB
testcase_03 AC 118 ms
8,296 KB
testcase_04 AC 849 ms
8,296 KB
testcase_05 AC 849 ms
8,420 KB
testcase_06 AC 848 ms
8,292 KB
testcase_07 AC 849 ms
8,420 KB
testcase_08 AC 927 ms
8,296 KB
testcase_09 AC 925 ms
8,420 KB
testcase_10 AC 922 ms
8,296 KB
testcase_11 AC 849 ms
8,424 KB
testcase_12 AC 851 ms
8,296 KB
testcase_13 AC 31 ms
8,164 KB
testcase_14 AC 32 ms
8,292 KB
testcase_15 AC 122 ms
8,192 KB
testcase_16 AC 125 ms
8,320 KB
testcase_17 AC 11 ms
6,272 KB
testcase_18 AC 301 ms
8,320 KB
testcase_19 AC 12 ms
6,400 KB
testcase_20 AC 596 ms
8,276 KB
testcase_21 AC 22 ms
8,048 KB
testcase_22 AC 848 ms
8,424 KB
testcase_23 AC 23 ms
8,044 KB
testcase_24 AC 23 ms
8,048 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():
    if hasattr(__builtins__,"raw_input"): input = raw_input
    if hasattr(__builtins__,"xrange"): range = xrange
    N = int(input())
    congs = [tuple([int(x) for x in 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