미래의 나를 위한 메모/코딩테스트

[230117] 백준 6064번 - 카잉달력 (Python)

Choi Jaekuk 2023. 1. 17. 16:41

https://github.com/cjk09083/Code

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

 

6064번: 카잉 달력

입력 데이터는 표준 입력을 사용한다. 입력은 T개의 테스트 데이터로 구성된다. 입력의 첫 번째 줄에는 입력 데이터의 수를 나타내는 정수 T가 주어진다. 각 테스트 데이터는 한 줄로 구성된다.

www.acmicpc.net

해답 1)

import sys
input = sys.stdin.readline


def gcd(a, b):
    return gcd(b, a % b) if b != 0 else a

if __name__ == "__main__": 
    n = int(input())
    ans = []
    for _ in range(n):
        M, N, x, y = map(int,input().split())
        # 최소 공배수
        end = int(M * N / gcd(M, N))

        result = -1

        for i in range(x,end+1,M):
            if (i-y) % N == 0:
                result = i
                break
        ans.append(result)
        # print(result)
        
    print(*ans, sep = "\n")
- M, N의 주기를 이용해 조건이 만족하는 i 값을 찾는다.
- 1096ms