結果

問題 No.187 中華風 (Hard)
ユーザー yaoshimaxyaoshimax
提出日時 2015-05-10 17:10:49
言語 PyPy2
(7.3.15)
結果
WA  
実行時間 -
コード長 989 bytes
コンパイル時間 1,361 ms
コンパイル使用メモリ 77,404 KB
実行使用メモリ 80,880 KB
最終ジャッジ日時 2023-09-19 09:45:11
合計ジャッジ時間 5,714 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 99 ms
78,988 KB
testcase_01 AC 93 ms
78,604 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 AC 75 ms
76,468 KB
testcase_18 AC 85 ms
77,848 KB
testcase_19 AC 78 ms
76,424 KB
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

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

0