File tree Expand file tree Collapse file tree 1 file changed +55
-0
lines changed
Expand file tree Collapse file tree 1 file changed +55
-0
lines changed Original file line number Diff line number Diff line change 1+ ``` java
2+ import java.io.* ;
3+ import java.util.* ;
4+
5+ public class boj1644 {
6+ static BufferedReader br = new BufferedReader (new InputStreamReader (System . in));
7+
8+ static int N ;
9+ static boolean [] isPrime;
10+ static boolean flag = false ;
11+ public static void main (String [] args ) throws Exception {
12+ N = Integer . parseInt(br. readLine());
13+ int start = 2 , end = 3 , res = 5 , answer = 0 ;
14+ isPrime = new boolean [N + 1 ];
15+ primeCheck();
16+ if (N == 5 ) answer = 1 ;
17+ if (isPrime[N ]) answer++ ;
18+ while (start < end) {
19+ if (res < N ) {
20+ end = nextIdx(end);
21+ res += end;
22+ } else if (res >= N ) {
23+ res -= start;
24+ start = nextIdx(start);
25+ }
26+ if (res == N && start != end) {
27+ answer++ ;
28+ }
29+ if (flag) break ;
30+ }
31+ System . out. println(answer);
32+
33+ }
34+ static void primeCheck () {
35+ Arrays . fill(isPrime, true );
36+ isPrime[0 ] = false ;
37+ isPrime[1 ] = false ;
38+ for (int i = 2 ; i <= Math . sqrt(N + 1 ); i++ ) {
39+ if (isPrime[i]) {
40+ for (int j = i* i; j <= N ; j+= i) {
41+ isPrime[j] = false ;
42+ }
43+ }
44+ }
45+ }
46+
47+ static int nextIdx (int idx ) {
48+ for (int i = idx+ 1 ; i <= N ; i++ ) {
49+ if (isPrime[i]) return i;
50+ }
51+ flag = true ;
52+ return idx;
53+ }
54+ }
55+ ```
You can’t perform that action at this time.
0 commit comments