結果

問題 No.187 中華風 (Hard)
ユーザー ayaoni
提出日時 2021-05-09 14:20:22
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 498 ms / 3,000 ms
コード長 1,076 bytes
コンパイル時間 253 ms
コンパイル使用メモリ 81,920 KB
実行使用メモリ 76,928 KB
最終ジャッジ日時 2024-09-18 20:32:56
合計ジャッジ時間 7,752 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 25
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

sys.setrecursionlimit(10**7)
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int,sys.stdin.readline().rstrip().split())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def LI2(): return list(map(int,sys.stdin.readline().rstrip()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())
def LS2(): return list(sys.stdin.readline().rstrip())


def ext_gcd(a,b):  # gcd(a,b) と a*x+b*y == gcd(a,b) の整数解(x,y)
    if b > 0:
        d,x,y = ext_gcd(b,a % b)
        return d,y,x-(a//b)*y
    return a,1,0


# V = [(Xi,Yi),…]

def remainder(V):  # Z == Xi (mod Yi) たる Z, lcm(Yi)
    x,lcm = 0,1
    for X,Y in V:
        g,a,b = ext_gcd(lcm,Y)
        x,lcm = (Y*b*x+lcm*a*X)//g,lcm*(Y//g)
        x %= lcm
    return x,lcm


N = I()
V = [tuple(MI()) for _ in range(N)]
mod = 10**9+7

ans,lcm = remainder(V)

for i in range(N):
    X,Y = V[i]
    if ans % Y != X:
        print(-1)
        break
else:
    if ans == 0:
        ans = lcm
    print(ans % mod)
0