結果

問題 No.187 中華風 (Hard)
コンテスト
ユーザー yuppe19 😺
提出日時 2015-06-22 12:56:36
言語 PyPy2
(7.3.15)
結果
AC  
実行時間 268 ms / 3,000 ms
コード長 777 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 134 ms
コンパイル使用メモリ 77,852 KB
最終ジャッジ日時 2025-12-03 15:40:10
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 25
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#!/usr/bin/python
from fractions import gcd

def extgcd(x, y):
    if y == 0:
        return 1, 0, x
    a, b, d = extgcd(y, x%y)
    return b, a-x/y*b, d

def mod_inv(a, div):
    x, y, d = extgcd(a, div)
    return x % div if d==1 else None

def linear_congruence(A, B, M):
    x, m = 0, 1
    for i in xrange(len(A)):
        a, b = A[i] * m, B[i] - A[i] * x
        d = gcd(M[i], a)
        if(b % d != 0):
            return 0, -1
        t = b / d * mod_inv(a/d, M[i]/d) % (M[i] / d)
        x = x + m * t
        m *= M[i] / d
    return x % m, m

N = int(raw_input())
xys = [map(int, raw_input().split()) for _ in xrange(N)]
zipped = zip(*xys)
res = linear_congruence([1] * N, *(zipped))
if res[1] == -1:
    print -1
else:
    print (res[0] or res[1]) % (int(1e9) + 7)
0