結果
| 問題 |
No.187 中華風 (Hard)
|
| コンテスト | |
| ユーザー |
onexisan
|
| 提出日時 | 2015-05-21 01:11:49 |
| 言語 | Python2 (2.7.18) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 759 bytes |
| コンパイル時間 | 90 ms |
| コンパイル使用メモリ | 6,912 KB |
| 実行使用メモリ | 8,576 KB |
| 最終ジャッジ日時 | 2024-07-06 04:17:59 |
| 合計ジャッジ時間 | 10,932 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 19 WA * 6 |
ソースコード
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:
_x, _y, _m, _n = x, y, m, n
g = gcd(m, n)
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
if x % _m != _x or x % _n != _y: return -1, -1
pass
return x, m
mod = 10 ** 9 + 7
n = input()
congs = [map(int, raw_input().split()) for _ in xrange(n)]
ans = CRT(congs)[0]
print -1 if ans == -1 else ans % mod
onexisan