結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 99 ms
78,692 KB
testcase_01 AC 95 ms
78,624 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 AC 185 ms
79,920 KB
testcase_05 AC 197 ms
79,992 KB
testcase_06 AC 181 ms
79,892 KB
testcase_07 AC 196 ms
80,020 KB
testcase_08 AC 196 ms
79,928 KB
testcase_09 AC 200 ms
79,768 KB
testcase_10 AC 197 ms
79,944 KB
testcase_11 AC 198 ms
80,768 KB
testcase_12 AC 196 ms
79,716 KB
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 AC 77 ms
76,548 KB
testcase_18 AC 83 ms
77,168 KB
testcase_19 AC 78 ms
76,528 KB
testcase_20 AC 179 ms
79,988 KB
testcase_21 AC 78 ms
76,440 KB
testcase_22 AC 198 ms
80,944 KB
testcase_23 AC 77 ms
76,452 KB
testcase_24 AC 79 ms
76,128 KB
権限があれば一括ダウンロードができます

ソースコード

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%1000000007

0