๋ฌธ์ (์ถ์ฒ: https://www.acmicpc.net/problem/1124)
< ์ธ๋ํ๋ผ์ >
๋ฌธ์ ํ์ด
๋จผ์ ์๋ผํ ์คํ ๋ค์ค์ ์ฒด๋ฅผ ์ฌ์ฉํ์ฌ ์์์ธ์ง ์๋์ง ํ๋จํ๋ค.
๊ทธ ํ์ A์ด์ B์ดํ์ ๊ฐ์ ๊ฐ๊ฐ ์์ธ์๋ถํดํ์ฌ ์์์ ๊ฐ์๋ฅผ ๊ตฌํ๋ค. ์์์ ๊ฐ์๊ฐ ์์์ธ์ง ํ๋จํ๋ค.
my solution (Java)
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class _1124_ { // ์ธ๋ํ๋ผ์
public static void main(String[] args) throws IOException {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(bf.readLine());
int a = Integer.parseInt(st.nextToken());
int b = Integer.parseInt(st.nextToken());
boolean arr[] = new boolean[100001];
arr[0]=true;
arr[1]=true;
for (int i = 2; i < 50001; i++) {
if (!arr[i]) {
for (int j = i + i; j < 100001; j += i) {
arr[j] = true;
}
}
}
int result = 0;
for (int i = a; i <= b; i++) {
int num = i;
int cnt = 0;
int idx = 2;
while (idx <= num) {
if (num % idx == 0) {
num /= idx;
cnt += 1;
} else {
idx++;
}
}
if (!arr[cnt]) {
result += 1;
}
}
System.out.println(result);
}
}
๋ณ์)
a, b : ๋ ์ ์
arr : ์์
result : ์ธ๋ํ๋ผ์ ๊ฐ์
num : ์์ฐ์ X
cnt : ์์ฐ์ X ์์ธ์๋ถํดํด์ ๊ตฌํ ์์์ ๋ชฉ๋ก์ ๊ธธ์ด
๋ ์ ์ A์ B๋ฅผ ์ ๋ ฅ๋ฐ๋๋ค.
๋จผ์ ์๋ผํ ์คํ ๋ค์ค์ ์ฒด๋ฅผ ์ฌ์ฉํด์ ์์๋ฅผ ํ๋ณํ ์ ์๊ฒ ๊ตฌํด๋๋ค.
๊ทธ๋ค์ A์ด์ B์ดํ์ ๊ฐ์ ๊ฐ๊ฐ ์์ธ์๋ถํดํ์ฌ ์์์ ๊ฐ์๋ฅผ ๊ตฌํ๋ค. ๊ทธ ์์์ ๊ฐ์๊ฐ ์์์ธ์ง ํ๋จํ๋ค.
'๐Algorithm > ๐ฅBaekjoon' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Baekjoon] 9421_์์์๊ทผ์ (1) | 2024.02.28 |
---|---|
[Baekjoon] 3896_์์ ์ฌ์ด ์์ด (1) | 2024.02.27 |
[Baekjoon] 2232_์ง๋ขฐ (0) | 2024.02.23 |
[Baekjoon] 3005_ํฌ๋ก์ค์๋ ํผ์ฆ ์ณ๋ค๋ณด๊ธฐ (0) | 2024.02.22 |
[Baekjoon] 1706_ํฌ๋ก์ค์๋ (0) | 2024.02.20 |