結果

問題 No.187 中華風 (Hard)
ユーザー mayoko_mayoko_
提出日時 2015-08-01 20:13:04
言語 PyPy2
(7.3.15)
結果
AC  
実行時間 449 ms / 3,000 ms
コード長 655 bytes
コンパイル時間 129 ms
コンパイル使用メモリ 77,676 KB
実行使用メモリ 81,128 KB
最終ジャッジ日時 2023-09-25 00:25:30
合計ジャッジ時間 7,587 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 91 ms
78,228 KB
testcase_01 AC 89 ms
78,228 KB
testcase_02 AC 222 ms
79,664 KB
testcase_03 AC 228 ms
79,868 KB
testcase_04 AC 425 ms
80,440 KB
testcase_05 AC 418 ms
79,400 KB
testcase_06 AC 422 ms
79,148 KB
testcase_07 AC 431 ms
81,128 KB
testcase_08 AC 442 ms
79,340 KB
testcase_09 AC 441 ms
79,660 KB
testcase_10 AC 449 ms
79,116 KB
testcase_11 AC 419 ms
79,296 KB
testcase_12 AC 416 ms
79,768 KB
testcase_13 AC 94 ms
78,760 KB
testcase_14 AC 94 ms
78,524 KB
testcase_15 AC 129 ms
79,236 KB
testcase_16 AC 141 ms
79,552 KB
testcase_17 AC 72 ms
75,824 KB
testcase_18 AC 86 ms
78,472 KB
testcase_19 AC 71 ms
76,036 KB
testcase_20 AC 322 ms
79,168 KB
testcase_21 AC 75 ms
75,444 KB
testcase_22 AC 421 ms
79,804 KB
testcase_23 AC 74 ms
75,680 KB
testcase_24 AC 72 ms
76,072 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
import math

def ext_gcd(p, q):
    if q==0:
        return (p,1,0)
    g,y,x = ext_gcd(q,p%q)
    y -= p/q*x
    return (g,x,y)

def cmt(a1,mo1,a2,mo2):
    g,x,y=ext_gcd(mo1,mo2)
    a1%=mo1
    a2%=mo2
    if a1%g != a2%g:
        return (-1,0)
    lcm=mo1*(mo2/g)
    v=a1+((a2-a1)%lcm)*x%lcm*(mo1/g)
    return (((v%lcm)+lcm)%lcm, lcm)

N=input()
P=[]
for i in range(N):
    P.append(map(int, raw_input().strip().split(" ")))

T=[P[0][0], P[0][1]]
for i in range(1, N):
    T2 = cmt(T[0], T[1], P[i][0], P[i][1])
    if (T2[0] < 0):
        print -1
        sys.exit()
    T=[T2[0],T2[1]]

if T[0]==0:
    T[0]=T[1]

print T[0]%1000000007
0