結果

問題 No.187 中華風 (Hard)
ユーザー yaoshimaxyaoshimax
提出日時 2015-05-10 22:50:45
言語 PyPy2
(7.3.15)
結果
AC  
実行時間 195 ms / 3,000 ms
コード長 832 bytes
コンパイル時間 2,044 ms
コンパイル使用メモリ 77,488 KB
実行使用メモリ 81,840 KB
最終ジャッジ日時 2023-09-20 01:47:56
合計ジャッジ時間 6,688 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 72 ms
76,572 KB
testcase_01 AC 73 ms
76,380 KB
testcase_02 AC 195 ms
80,856 KB
testcase_03 AC 163 ms
80,096 KB
testcase_04 AC 188 ms
80,924 KB
testcase_05 AC 191 ms
81,840 KB
testcase_06 AC 161 ms
80,252 KB
testcase_07 AC 192 ms
80,772 KB
testcase_08 AC 186 ms
80,264 KB
testcase_09 AC 182 ms
80,068 KB
testcase_10 AC 181 ms
79,992 KB
testcase_11 AC 171 ms
80,916 KB
testcase_12 AC 179 ms
80,760 KB
testcase_13 AC 93 ms
79,156 KB
testcase_14 AC 90 ms
79,144 KB
testcase_15 AC 183 ms
81,332 KB
testcase_16 AC 175 ms
79,912 KB
testcase_17 AC 71 ms
76,372 KB
testcase_18 AC 72 ms
76,700 KB
testcase_19 AC 71 ms
76,468 KB
testcase_20 AC 164 ms
80,248 KB
testcase_21 AC 72 ms
76,248 KB
testcase_22 AC 166 ms
79,924 KB
testcase_23 AC 72 ms
76,472 KB
testcase_24 AC 72 ms
76,168 KB
権限があれば一括ダウンロードができます

ソースコード

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)%Y)/r*B+A
    B*=Y/gcd(Y,B)
    if A<X:
         A=X
print A%1000000007

0