結果

問題 No.186 中華風 (Easy)
ユーザー ayaoniayaoni
提出日時 2021-05-02 03:14:25
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 42 ms / 2,000 ms
コード長 1,047 bytes
コンパイル時間 225 ms
コンパイル使用メモリ 81,852 KB
実行使用メモリ 52,480 KB
最終ジャッジ日時 2024-07-20 11:25:56
合計ジャッジ時間 1,917 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 23
権限があれば一括ダウンロードができます

ソースコード

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


V = [tuple(MI()) for _ in range(3)]
ans,lcm = remainder(V)

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