結果

問題 No.187 中華風 (Hard)
ユーザー yaoshimax
提出日時 2015-05-10 22:46:24
言語 PyPy2
(7.3.15)
結果
WA  
実行時間 -
コード長 834 bytes
コンパイル時間 1,012 ms
コンパイル使用メモリ 76,288 KB
実行使用メモリ 78,848 KB
最終ジャッジ日時 2024-07-05 22:07:00
合計ジャッジ時間 5,038 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 21 WA * 4
権限があれば一括ダウンロードができます

ソースコード

diff #

def extgcd(A,B):
    #return p,q,r such that Ap+Bq=r
    if A>B:
        q,p,r=extgcd(B,A)
        return p,q,r
    if A==0:
        return 0,1,B
    # B=cA+R then Ap+Bq=Ap+(cA+R)q=(cq+p)A+qR
    c=B/A
    R=B%A
    q,cqp,r=extgcd(R,A)
    return cqp-c*q,q,r

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())
    # find smallest A+Bp==X+Yq <=Bp-Yq=X-A
    #print "find ",A,"+",B,"p==",X,"+",Y,"q"
    p,q,r=extgcd(B,Y)
    #print B,"*",p,"+",Y,"*",q,"=",r
    if abs(X-A)%r!=0:
        print -1
        exit()
    #now Bp+Yq=1
    #print A,"+",B*Y,(p*(X-A))%Y 
    A=((p*(X-A)/r)%Y)*B+A
    B*=Y/gcd(Y,B)
    if A<X:
         A=X
print A%1000000007

0