반응형

문제

 

소스코드

 

# 최대공약수
def gcd(a, b):
    while b:
        temp = a % b
        a = b
        b = temp
    return abs(a)

# 최소공배수
def lcm(a, b):
    gcd_value = gcd(a, b)
    return abs((a * b) / gcd_value)


t = int(input())
for i in range(t):
    m, n, x, y = map(int, input().split())
    cnt = x % (m+1)
    tempY = x
    is_valid = False
    for j in range(n):
        ty = n if tempY % n == 0 else (tempY % n)
        if ty == y:
            is_valid = True
            break
        cnt += m
        tempY = ty + m
    print(cnt if is_valid else -1)

 

반응형
반응형