Baekjoon Case

[파이썬 / 백준 6064번] 카잉 달력

Scarlett_C 2021. 11. 22. 10:47
728x90

https://www.acmicpc.net/problem/6064

728x90
def fm(a,b):
    while True:
        r=a%b
        if r==0:
            return b
            break
        else:
            a,b=b,r
from sys import stdin
input=stdin.readline
T=int(input())
for i in range(T):
    M,N,x,y=map(int,input().split())
    md=fm(max(M,N),min(M,N))
    mx=N//md
    for i in range(mx+1):
        a=M*i+x
        b=a%N
        if b==0: b=N
        if b==y:
           print(a)
           break
        if i==mx: print(-1)
728x90