結果
| 問題 | No.187 中華風 (Hard) |
| コンテスト | |
| ユーザー |
yaoshimax
|
| 提出日時 | 2015-05-10 17:10:49 |
| 言語 | PyPy2 (7.3.15) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 989 bytes |
| 記録 | |
| コンパイル時間 | 116 ms |
| コンパイル使用メモリ | 77,056 KB |
| 実行使用メモリ | 78,976 KB |
| 最終ジャッジ日時 | 2024-07-05 21:27:50 |
| 合計ジャッジ時間 | 4,045 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 5 WA * 20 |
ソースコード
def extgcd(A,B):
#return p,q such that Ap+Bq=1
if A>B:
q,p=extgcd(B,A)
return p,q
if A==0:
if B==1:
return 0,1
else:
return 0,0
# B=cA+R then Ap+Bq=Ap+(cA+R)q=(cq+p)A+qR
c=B/A
R=B%A
q,cqp=extgcd(R,A)
return cqp-c*q,q
def gcd(A,B):
if B<A:
return gcd(B,A)
if B%A==0:
return A
return gcd(B%A,A)
N=int(raw_input())
A,B=map(int,raw_input().split())
if A==0:
A=B
for i in range(N-1):
X,Y=map(int,raw_input().split())
if B>Y and B%Y==0 and A%B!=X%Y:
print -1
exit()
elif B<Y and Y%B==0 and A%B!=X%Y:
print -1
exit()
else:
# find smallest A+Bp==X+Yq <=Bp-Yq=X-A
g=gcd(B,Y)
Y/=g
#print "find ",A,"+",B,"p==",X,"+",Y,"q"
p,q=extgcd(B,Y)
#now Bp+Yq=1
#print A,"+",B*Y,(p*(X-A))%Y
A=(p*(X-A)%Y)*B+A
B*=Y
if A<X:
A=X
print A%1000000009
yaoshimax