File tree Expand file tree Collapse file tree 1 file changed +50
-0
lines changed
Expand file tree Collapse file tree 1 file changed +50
-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 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+ ```
You can’t perform that action at this time.
0 commit comments