結果

問題 No.186 中華風 (Easy)
ユーザー 👑 SPD_9X2SPD_9X2
提出日時 2021-03-03 14:15:16
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 45 ms / 2,000 ms
コード長 1,056 bytes
コンパイル時間 171 ms
コンパイル使用メモリ 82,160 KB
実行使用メモリ 54,584 KB
最終ジャッジ日時 2024-04-14 06:59:37
合計ジャッジ時間 2,293 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
54,284 KB
testcase_01 AC 45 ms
54,096 KB
testcase_02 AC 44 ms
53,140 KB
testcase_03 AC 41 ms
53,068 KB
testcase_04 AC 40 ms
52,688 KB
testcase_05 AC 40 ms
52,664 KB
testcase_06 AC 40 ms
53,568 KB
testcase_07 AC 40 ms
52,876 KB
testcase_08 AC 40 ms
53,184 KB
testcase_09 AC 40 ms
53,128 KB
testcase_10 AC 40 ms
53,072 KB
testcase_11 AC 40 ms
54,584 KB
testcase_12 AC 44 ms
53,200 KB
testcase_13 AC 44 ms
52,588 KB
testcase_14 AC 44 ms
52,892 KB
testcase_15 AC 44 ms
52,936 KB
testcase_16 AC 40 ms
53,132 KB
testcase_17 AC 41 ms
52,948 KB
testcase_18 AC 40 ms
53,664 KB
testcase_19 AC 40 ms
53,236 KB
testcase_20 AC 41 ms
53,396 KB
testcase_21 AC 40 ms
54,120 KB
testcase_22 AC 40 ms
52,324 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

"""

時刻tを求める

X <= t < X+Y (mod 2X+2Y)
P <= t (mod P+Q)

それぞれに関して中国剰余

"""

import sys
from sys import stdin

import math
def extGCD(a,b):
    g = math.gcd(a,b)
    x, y, u, v = 1, 0, 0, 1
    while b:
        k = a // b
        x -= k * u
        y -= k * v
        x, u = u, x
        y, v = v, y
        a, b = b, a % b
    return g ,x, y

def crt2(b1,m1,b2,m2):
    g,p,q = extGCD(m1,m2)
    if b1 % g != b2 % g:
        return 0,0
    return ( b1 + m1 * ((b2-b1)//g) * p ) % (m1*m2//g) , m1*m2//g

def crt(b,m):
    assert len(b) == len(m)
    
    nb,nm = 0,1
    for i in range(len(b)):
        nb,nm = crt2(nb,nm,b[i],m[i])
        if (nb,nm) == (0,0):
            return (0,0)
    return nb,nm


TT = 1

for loop in range(TT):

    N = 3
    B = []
    M = []

    for i in range(N):
        x,y = map(int,stdin.readline().split())
        B.append(x)
        M.append(y)

    ans,lcm = crt(B,M)
    if (ans,lcm) == (0,0):
        print (-1)
    elif ans == 0:
        print (lcm)
    else:
        print (ans)
0