-
Notifications
You must be signed in to change notification settings - Fork 0
/
1257.java
executable file
·70 lines (63 loc) · 1.13 KB
/
1257.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import java.util.Scanner;
public class Main {
static boolean[] primes;
static void primeSet(final int MAX){
primes = new boolean[MAX+1];
primes[2] = true;
for(int i=3;i<=MAX;i+=2){
primes[i] = true;
}
int rt = (int) Math.sqrt(MAX);
for(int i=3;i<=rt;i+=2){
if(primes[i]){
for(int j=i*i;j<=MAX;j+=i){
primes[j] = false;
}
}
}
}
public static void main(String[] args) {
Scanner stdIn = new Scanner(System.in);
int p = 0;
while(true){
int n = stdIn.nextInt();
if(p<n){
primeSet(n);
}
if(n==0){
return;
}
int before = 0;
int cnt = 0;
for(int i=2;i<=n;i++){
int sum=0;
int start = 0;
for(int j=i;j<=n;j++){
if(primes[j]){
if(start==0){
start=j;
}
sum+=j;
if(sum==n){
if(start==before){
sum=0;
start=0;
break;
}
before=start;
start=0;
cnt++;
sum=0;
break;
}
else if(sum>n){
sum=0;
break;
}
}
}
}
System.out.println(cnt);
}
}
}