Skip to content

Commit 7f49a19

Browse files
authored
[20260121] BOJ / G3 / 소수의 연속합 / 이준희
1 parent b55bb42 commit 7f49a19

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
```java
2+
import java.io.*;
3+
import java.util.*;
4+
5+
public class Main {
6+
public static void main(String[] args) throws IOException {
7+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
8+
int N = Integer.parseInt(br.readLine());
9+
10+
if (N == 1) {
11+
System.out.println(0);
12+
return;
13+
}
14+
15+
boolean[] isPrime = new boolean[N + 1];
16+
Arrays.fill(isPrime, true);
17+
isPrime[0] = isPrime[1] = false;
18+
19+
for (int i = 2; i * i <= N; i++) {
20+
if (isPrime[i]) {
21+
for (int j = i * i; j <= N; j += i) {
22+
isPrime[j] = false;
23+
}
24+
}
25+
}
26+
27+
List<Integer> primes = new ArrayList<>();
28+
for (int i = 2; i <= N; i++) {
29+
if (isPrime[i]) primes.add(i);
30+
}
31+
32+
int count = 0;
33+
int start = 0, end = 0, sum = 0;
34+
int size = primes.size();
35+
36+
while (true) {
37+
if (sum >= N) {
38+
if (sum == N) count++;
39+
sum -= primes.get(start++);
40+
} else if (end == size) {
41+
break;
42+
} else {
43+
sum += primes.get(end++);
44+
}
45+
}
46+
47+
System.out.println(count);
48+
}
49+
}
50+
```

0 commit comments

Comments
 (0)