結果

問題 No.187 中華風 (Hard)
ユーザー kimiyukikimiyuki
提出日時 2016-12-29 23:38:25
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 832 ms / 3,000 ms
コード長 752 bytes
コンパイル時間 353 ms
コンパイル使用メモリ 10,788 KB
実行使用メモリ 9,256 KB
最終ジャッジ日時 2023-08-22 00:13:12
合計ジャッジ時間 11,528 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 396 ms
9,204 KB
testcase_01 AC 391 ms
9,256 KB
testcase_02 AC 67 ms
9,020 KB
testcase_03 AC 66 ms
9,124 KB
testcase_04 AC 760 ms
9,100 KB
testcase_05 AC 758 ms
9,064 KB
testcase_06 AC 759 ms
9,140 KB
testcase_07 AC 762 ms
9,208 KB
testcase_08 AC 830 ms
9,228 KB
testcase_09 AC 829 ms
9,096 KB
testcase_10 AC 832 ms
9,208 KB
testcase_11 AC 764 ms
9,248 KB
testcase_12 AC 764 ms
9,040 KB
testcase_13 AC 25 ms
9,004 KB
testcase_14 AC 25 ms
9,004 KB
testcase_15 AC 78 ms
8,976 KB
testcase_16 AC 80 ms
8,960 KB
testcase_17 AC 21 ms
8,748 KB
testcase_18 AC 263 ms
9,116 KB
testcase_19 AC 21 ms
8,808 KB
testcase_20 AC 527 ms
9,068 KB
testcase_21 AC 20 ms
8,748 KB
testcase_22 AC 760 ms
9,208 KB
testcase_23 AC 20 ms
8,744 KB
testcase_24 AC 21 ms
8,804 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/usr/bin/env python3
import functools
import math
lcm = lambda a, b: a * b // math.gcd(a, b)
def extgcd(a, b):
    if b == 0:
        return 1, 0
    else:
        na, nb = extgcd(b, a % b)
        return nb, na - a//b * nb
def modinv(a, n):
    return extgcd(a, n)[0] % n
def crt(eqn1, eqn2):
    x1, m1 = eqn1
    x2, m2 = eqn2
    d = math.gcd(m1, m2)
    x = x1 + (m1 // d) * (x2 - x1) * modinv(m1 // d, m2 // d)
    m = lcm(m1, m2)
    return x % m, m
n = int(input())
eqn = [ tuple(map(int, input().split())) for _ in range(n) ]
ans, _ = functools.reduce(crt, eqn)
if ans == 0:
    ans += functools.reduce(lcm, map(lambda it: it[1], eqn))
for x, y in eqn:
    if ans % y != x:
        ans = -1
        break
else:
    ans %= 10**9+7
print(ans)
0