[백준] 25487번 - 단순한 문제 (Large) (Silver 3)
업데이트:
문제 링크Permalink
백준 25487번 - 단순한 문제 (Large) (Silver 3)
문제 설명Permalink
세 개의 자연수 a, b, c가 주어져 있다. 다음 조건을 만족하는 정수 쌍 (x, y, z)의 개수를 구하시오.
- 1≤x≤a
- 1≤y≤b
- 1≤z≤c
- x%y=y%z=z%x
정답 코드 및 설명Permalink
마지막 조건식이 무슨 의미인지 생각해보자.
x,y,z 중 가장 작은 수가 x라고 가정하면, z%x<x이다.
x<y 라면 x%y=x이므로 모순. 따라서 x=y 이다.
비슷한 방식으로, 마지막 조건식과 x=y=z가 동치임을 보일 수 있다.
따라서 조건을 만족하는 정수 쌍 (x, y, z)의 개수는 a, b, c의 최솟값과 같다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import java.io.*;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int t = Integer.parseInt(br.readLine());
StringTokenizer st;
int a;
while (t-- > 0) {
st = new StringTokenizer(br.readLine());
a = Integer.parseInt(st.nextToken());
a = Math.min(a, Integer.parseInt(st.nextToken()));
a = Math.min(a, Integer.parseInt(st.nextToken()));
bw.write(a + "\n");
}
bw.close();
}
}
댓글남기기