Scroll indicator done
728x90


#include <iostream>	
#include <algorithm>	
using namespace std;

int m, n, x, y;
int g(int a, int b) {
	if (a % b == 0) return b;
	else return g(b, a % b);
}

int l(int a, int b) {
	return (a * b) / g(a, b);
}

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
	int t;
	cin >> t;
	for (int i = 0; i < t; i++) {
		cin >> m >> n >> x >> y;
		int maxY = l(m, n);

		while (1) {
			if (x > maxY || (x - 1) % n + 1 == y) 
				break;
			x += m;
		}
		if (x > maxY) cout << -1 << '\n';
		else cout << x << '\n';
	}
	return 0;
}
728x90

'BAEKJOON > C++' 카테고리의 다른 글

[B11286][절댓값 힙][C++]  (0) 2021.10.27
[B7569][토마토][C++]  (0) 2021.08.17
[B2667][단지 번호 붙이기][C++]  (0) 2021.07.20
[B2178][미로 탐색][C++]  (0) 2021.07.16
[B1992][쿼드트리][C++]  (0) 2021.07.14