結果

問題 No.187 中華風 (Hard)
ユーザー peroonperoon
提出日時 2020-12-22 15:07:41
言語 PyPy2
(7.3.15)
結果
AC  
実行時間 510 ms / 3,000 ms
コード長 1,213 bytes
コンパイル時間 122 ms
コンパイル使用メモリ 77,644 KB
実行使用メモリ 82,016 KB
最終ジャッジ日時 2023-10-21 12:56:06
合計ジャッジ時間 8,051 ms
ジャッジサーバーID
(参考情報)
judge15 / judge10
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 94 ms
78,760 KB
testcase_01 AC 93 ms
78,764 KB
testcase_02 AC 203 ms
80,668 KB
testcase_03 AC 234 ms
81,352 KB
testcase_04 AC 470 ms
81,944 KB
testcase_05 AC 464 ms
81,092 KB
testcase_06 AC 474 ms
80,840 KB
testcase_07 AC 481 ms
80,964 KB
testcase_08 AC 505 ms
81,916 KB
testcase_09 AC 507 ms
81,944 KB
testcase_10 AC 510 ms
82,016 KB
testcase_11 AC 487 ms
81,088 KB
testcase_12 AC 476 ms
80,956 KB
testcase_13 AC 117 ms
78,916 KB
testcase_14 AC 124 ms
78,936 KB
testcase_15 AC 201 ms
80,892 KB
testcase_16 AC 193 ms
80,600 KB
testcase_17 AC 77 ms
76,260 KB
testcase_18 AC 91 ms
78,760 KB
testcase_19 AC 77 ms
76,260 KB
testcase_20 AC 377 ms
80,300 KB
testcase_21 AC 77 ms
76,260 KB
testcase_22 AC 482 ms
81,896 KB
testcase_23 AC 77 ms
76,260 KB
testcase_24 AC 77 ms
76,260 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

# cf
# http://techtipshoge.blogspot.com/2015/02/blog-post_15.html

def gcd(a, b):
    if b == 0:
        return a
    return gcd(b, a%b)

# return [g, x, y]
# g = gcd(a, b)
# x, y satisfies a x + b y = g
def extgcd(a, b):
    if b == 0:
        return [a, 1, 0]
    g, x, y = extgcd(b, a%b)
    return [g, y, x - a/b * y]

# eq0: x = a0 (mod m0)
# eq1: x = a1 (mod m1)
# returns [xt, mod] such that x = xt + k mod for integer k.
def crt(eq0, eq1):
    a0, m0 = eq0
    a1, m1 = eq1

    g = gcd(m0, m1)

    if a0 % g != a1 % g:
        #raise Exception("x doesn't exist.")
        print(-1)
        sys.exit()

    if g > 1:
        m0 /= g
        m1 /= g

        while True:
            gt = gcd(m0, g)
            if gt == 1:
                break
            m0 *= gt
            g /= gt
        
        m1 *= g

        a0 %= m0
        a1 %= m1

    g, p, q = extgcd(m0, m1)
    
    x = a0 * q * m1 + a1 * p * m0
    mod = m0 * m1
    x = x % mod

    return [x, mod]

n = int(raw_input())
eqs = []
for _ in xrange(n):
    a, m = map(int, raw_input().split())
    eqs.append((a, m))

# solve the system
x, mod = reduce(crt, eqs, (0, 1))
if x==0:
    x += mod

MOD = 1000000007
print(x % MOD)
0