結果

問題 No.187 中華風 (Hard)
ユーザー orisanoorisano
提出日時 2016-05-28 14:39:00
言語 PyPy2
(7.3.15)
結果
AC  
実行時間 697 ms / 3,000 ms
コード長 1,051 bytes
コンパイル時間 1,393 ms
コンパイル使用メモリ 76,800 KB
実行使用メモリ 93,824 KB
最終ジャッジ日時 2024-04-16 18:10:15
合計ジャッジ時間 13,592 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 450 ms
92,672 KB
testcase_01 AC 449 ms
92,672 KB
testcase_02 AC 283 ms
92,416 KB
testcase_03 AC 281 ms
92,672 KB
testcase_04 AC 657 ms
93,440 KB
testcase_05 AC 670 ms
93,568 KB
testcase_06 AC 665 ms
93,440 KB
testcase_07 AC 663 ms
93,568 KB
testcase_08 AC 697 ms
93,568 KB
testcase_09 AC 693 ms
93,824 KB
testcase_10 AC 694 ms
93,696 KB
testcase_11 AC 664 ms
93,184 KB
testcase_12 AC 667 ms
93,312 KB
testcase_13 AC 199 ms
91,392 KB
testcase_14 AC 239 ms
91,136 KB
testcase_15 AC 290 ms
92,288 KB
testcase_16 AC 291 ms
92,672 KB
testcase_17 AC 90 ms
75,648 KB
testcase_18 AC 368 ms
92,672 KB
testcase_19 AC 93 ms
75,520 KB
testcase_20 AC 535 ms
92,800 KB
testcase_21 AC 159 ms
81,664 KB
testcase_22 AC 666 ms
93,440 KB
testcase_23 AC 158 ms
81,536 KB
testcase_24 AC 160 ms
81,792 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"): __builtins__.input = raw_input
    if hasattr(__builtins__,"xrange"): __builtins__.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