-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMain.java
47 lines (41 loc) Β· 1.24 KB
/
Main.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
package Prime.P1644;
import java.io.*;
import java.util.*;
public class Main {
static int N;
static List<Integer> primes = new ArrayList<>();
public static void main(String[] args) throws Exception {
System.setIn(new FileInputStream("src/Prime/P1644/input.txt"));
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
N = Integer.parseInt(br.readLine());
if (N == 1) System.out.println(0);
else {
makePrimes();
System.out.println(solve());
}
}
static int solve() {
int low = 0, high = 0, len = primes.size(), sum = 2, cnt = 0;
while (low <= high) {
if (sum >= N) {
if (sum == N) cnt ++;
sum -= primes.get(low++);
} else {
if (++high >= len) break;
sum += primes.get(high);
}
}
return cnt;
}
static void makePrimes() {
boolean[] primeCheck = new boolean[N+1];
for (int i = 2; i <= N; i++) {
if (!primeCheck[i]) {
primes.add(i);
for (int j = 2; i*j <= N; j++) {
primeCheck[i*j] = true;
}
}
}
}
}