結果

問題 No.187 中華風 (Hard)
ユーザー onexisanonexisan
提出日時 2015-05-21 01:17:05
言語 Python2
(2.7.18)
結果
AC  
実行時間 1,594 ms / 3,000 ms
コード長 764 bytes
コンパイル時間 524 ms
コンパイル使用メモリ 6,696 KB
実行使用メモリ 8,152 KB
最終ジャッジ日時 2023-09-20 08:35:43
合計ジャッジ時間 15,149 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 26 ms
7,676 KB
testcase_01 AC 26 ms
7,776 KB
testcase_02 AC 836 ms
7,908 KB
testcase_03 AC 803 ms
7,936 KB
testcase_04 AC 748 ms
7,964 KB
testcase_05 AC 746 ms
7,980 KB
testcase_06 AC 750 ms
8,068 KB
testcase_07 AC 753 ms
7,996 KB
testcase_08 AC 815 ms
7,920 KB
testcase_09 AC 816 ms
8,040 KB
testcase_10 AC 819 ms
7,932 KB
testcase_11 AC 751 ms
8,120 KB
testcase_12 AC 751 ms
7,940 KB
testcase_13 AC 30 ms
7,884 KB
testcase_14 AC 30 ms
7,712 KB
testcase_15 AC 1,560 ms
8,104 KB
testcase_16 AC 1,594 ms
7,880 KB
testcase_17 AC 23 ms
7,648 KB
testcase_18 AC 26 ms
7,860 KB
testcase_19 AC 25 ms
7,792 KB
testcase_20 AC 522 ms
8,152 KB
testcase_21 AC 24 ms
7,672 KB
testcase_22 AC 749 ms
7,960 KB
testcase_23 AC 24 ms
7,824 KB
testcase_24 AC 24 ms
7,720 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from fractions import gcd

def inv(a, m):
    b, x, y = m, 1, 0
    while b != 0:
        t = a // b
        a, b = b, a - t * b
        x, y = y, x - t * y
        pass
    return x % m

def CRT(congs):
    x, m = 0, 1
    for y, n in congs:
        _y, _n = y, n
        g = gcd(m, n)
        if x % g != y % g: return -1, -1
        m //= g; l = m * n
        _m = m
        while True:
            t = gcd(m * _m, l)
            if t == m: break
            m = t
        n //= gcd(m, n)
        x %= m; x += (y - x % n) * inv(m, n) % n * m; m *= n
        pass
    return x, m

mod = 10 ** 9 + 7

n = input()
congs = [map(int, raw_input().split()) for _ in xrange(n)]

ans = CRT(congs)[0]
if ans == 0: ans = CRT(congs)[1]
print -1 if ans == -1 else ans % mod
0