6064번: 카잉 달력
입력 데이터는 표준 입력을 사용한다. 입력은 T개의 테스트 데이터로 구성된다. 입력의 첫 번째 줄에는 입력 데이터의 수를 나타내는 정수 T가 주어진다. 각 테스트 데이터는 한 줄로 구성된다.
www.acmicpc.net
- 풀이
처음에 문제풀 때 M*N의 시간복잡도로 풀어 시간초과가 났습니다.
해결방법은 유클리드 호제법을 통해 계산해야되는 경우의 수를 최대한 줄여줍니다.
해당 방법과 반복문을 최대한으로 줄이기 위해 i*M<lcm의 방식을 사용했습니다.
(i*M+x-y) % N ==0 이라면 x 값도 나오고 y 값도 나온다는 것을 의미하기 때문에 index를 초기화 시켜준 뒤 break를 걸어주었습니다.
이런 식의 반복문을 사용한다면 반복문의 최대 반복수는 lcm%M 번으로 줄일 수 있습니다.
- 코드
import java.io.IOException;
import java.io.InputStream;
import java.util.*;
public class Main {
static StringBuilder sb;
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
SetData();
System.out.println(sb);
}
private static void SetData() throws Exception {
InputReader in = new InputReader(System.in);
int testcase = in.nextInt();
sb = new StringBuilder();
for (int i = 0; i < testcase; i++) {
int M = in.nextInt();
int N = in.nextInt();
int x = in.nextInt();
int y = in.nextInt();
int lcm = M * N / gcd(M, N);
sb.append(FindValue(lcm, M, N, x, y)).append("\n");
}
}
private static int gcd(int x, int y) {
if (x % y == 0)
return y;
return gcd(y, x % y);
}
private static int FindValue(int lcm, int M, int N, int x, int y) {
int answer = -1;
for (int i = 0; i * M < lcm; i++) {
if ((i * M + x - y) % N == 0) {
answer = i * M + x;
break;
}
}
return answer;
}
}
class InputReader {
private final InputStream stream;
private final byte[] buf = new byte[8192];
private int curChar, snumChars;
public InputReader(InputStream st) {
this.stream = st;
}
public int read() {
if (snumChars == -1)
throw new InputMismatchException();
if (curChar >= snumChars) {
curChar = 0;
try {
snumChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (snumChars <= 0)
return -1;
}
return buf[curChar++];
}
public int nextInt() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public long nextLong() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public int[] nextIntArray(int n) {
int a[] = new int[n];
for (int i = 0; i < n; i++) {
a[i] = nextInt();
}
return a;
}
public String nextLine() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isEndOfLine(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private boolean isEndOfLine(int c) {
return c == '\n' || c == '\r' || c == -1;
}
}
'algorithm' 카테고리의 다른 글
[BOJ/JAVA] 백준 1167번 : 트리의 지름 (0) | 2021.10.25 |
---|---|
[BOJ/JAVA] 백준 1939번 : 중량제한 (0) | 2021.10.23 |
[BOJ/JAVA] 백준 9019번 : DSLR (0) | 2021.10.22 |
[BOJ/JAVA] 백준 13023번 : ABCDE (0) | 2021.10.21 |
[BOJ/JAVA] 백준 1261번 : 알고스팟 (0) | 2021.10.20 |