結果
問題 |
No.187 中華風 (Hard)
|
ユーザー |
![]() |
提出日時 | 2016-05-28 14:36:28 |
言語 | Python3 (3.13.1 + numpy 2.2.1 + scipy 1.14.1) |
結果 |
RE
|
実行時間 | - |
コード長 | 1,025 bytes |
コンパイル時間 | 245 ms |
コンパイル使用メモリ | 12,672 KB |
実行使用メモリ | 10,880 KB |
最終ジャッジ日時 | 2024-10-07 17:29:09 |
合計ジャッジ時間 | 1,811 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | RE * 25 |
ソースコード
from functools import * def egcd(a, b): x, y, u, v = 0, 1, 1, 0 while a != 0: q, r = b // a, b % a m, n = x - u * q, y - v * q b, a, x, y, u, v = a, r, u, v, m, n gcd = b return gcd, x, y def lcm(a, b): from fractions import gcd return a // gcd(a, b) * b def gcrt(congs): def fn(cong, prod): a, n = cong m = prod // n g, x, _ = egcd(m, n) return m // g * x * a def crt(a, b): N = a[1] * b[1] L = lcm(a[1], b[1]) return (fn(a, N) + fn(b, N)) % L, L return reduce(crt, congs) def main(): if hasattr(__builtins__,"raw_input"): input = raw_input if hasattr(__builtins__,"xrange"): range = xrange N = int(input()) congs = [tuple([int(x) for x in input().split(" ")]) for _ in range(N)] ans, mod = gcrt(congs) if ans == 0: ans = mod if any(ans % m != a for a, m in congs): print(-1) else: print(ans % (10**9 + 7)) if __name__ == "__main__": main()