-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBaekjoon6064.java
69 lines (58 loc) · 1.37 KB
/
Baekjoon6064.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package Algorithms;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
/**
* https://www.acmicpc.net/problem/6064
* 백준 카잉달력
*/
public class Baekjoon6064 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int testCase = sc.nextInt();
while (testCase-->0){
int M = sc.nextInt();
int N = sc.nextInt();
int x = sc.nextInt();
int y = sc.nextInt();
solution(M, N, x, y );
}
}
private static void solution(int m, int n, int x, int y) {
int num = 1;
int i = 1;
int j = 1;
while (i!=x){
i++;
j++;
if(i==m+1) i=1;
if(j==n+1) j=1;
num++;
}
int count = num;
int limit = lcm(m,n);
while(j!=y){
if(count>limit){
System.out.println(-1);
return;
}
j = (j+m)%n;
if(j==0){
j=n;
}
count+=m;
}
System.out.println(count);
}
public static int gcd(int a, int b) {
while (b != 0) {
int r = a % b;
a = b;
b = r;
}
return a;
}
public static int lcm(int a, int b) {
return a * b / gcd(a, b);
}
}