結果

問題 No.187 中華風 (Hard)
ユーザー orisanoorisano
提出日時 2016-05-28 14:38:28
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 901 ms / 3,000 ms
コード長 1,046 bytes
コンパイル時間 98 ms
コンパイル使用メモリ 11,088 KB
実行使用メモリ 9,344 KB
最終ジャッジ日時 2023-10-12 14:38:23
合計ジャッジ時間 12,878 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 432 ms
9,276 KB
testcase_01 AC 420 ms
9,228 KB
testcase_02 AC 105 ms
9,292 KB
testcase_03 AC 102 ms
9,340 KB
testcase_04 AC 827 ms
9,344 KB
testcase_05 AC 827 ms
9,292 KB
testcase_06 AC 826 ms
9,176 KB
testcase_07 AC 828 ms
9,292 KB
testcase_08 AC 899 ms
9,296 KB
testcase_09 AC 900 ms
9,280 KB
testcase_10 AC 901 ms
9,316 KB
testcase_11 AC 827 ms
9,252 KB
testcase_12 AC 831 ms
9,196 KB
testcase_13 AC 28 ms
9,052 KB
testcase_14 AC 28 ms
8,964 KB
testcase_15 AC 106 ms
9,200 KB
testcase_16 AC 109 ms
9,252 KB
testcase_17 AC 21 ms
8,744 KB
testcase_18 AC 289 ms
9,160 KB
testcase_19 AC 21 ms
8,656 KB
testcase_20 AC 580 ms
9,320 KB
testcase_21 AC 22 ms
8,856 KB
testcase_22 AC 830 ms
9,220 KB
testcase_23 AC 21 ms
8,904 KB
testcase_24 AC 21 ms
8,872 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 math 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