結果

問題 No.187 中華風 (Hard)
ユーザー uwiuwi
提出日時 2015-04-19 23:35:59
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 767 ms / 3,000 ms
コード長 543 bytes
コンパイル時間 77 ms
コンパイル使用メモリ 12,544 KB
実行使用メモリ 11,008 KB
最終ジャッジ日時 2024-07-04 18:29:57
合計ジャッジ時間 9,161 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 25
権限があれば一括ダウンロードができます

ソースコード

diff #

def exgcd(a, b):
	p = 1; q = 0; r = 0; s = 1;
	while b > 0:
		c = a//b
		d = a; a = b; b = d % b;
		d = p; p = q; q = d - c * q;
		d = r; r = s; s = d - c * s;
	return (a, p, r)

def crt(p, m, q, n):
	a,d,r = exgcd(p, q)
	if (n-m) % a != 0: return (-1,-1)
	mod = p*(q//a)
	a = (d*((n-m)//a)*p+m)%mod
	if a < 0: a += mod
	return (a,mod)

import sys
n = int(input())
xx = 0; yy = 1;
for _ in range(n):
	x,y = map(int,input().split())
	xx,yy = crt(y, x, yy, xx)
	if xx == -1:
		print(-1)
		sys.exit(0)
if xx == 0:
	xx = yy
print(xx % 1000000007)
0