Skip to content

Instantly share code, notes, and snippets.

@kunishi
Created July 2, 2012 03:59
Show Gist options
  • Save kunishi/3030994 to your computer and use it in GitHub Desktop.
Save kunishi/3030994 to your computer and use it in GitHub Desktop.
ACM International Collegiate Programming Contest, Asia Regional (Tokyo), 2005, Problem A
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
public class Asia2005A {
public static void main(String[] args) throws FileNotFoundException {
boolean[] isprime = new boolean[10000];
ArrayList<Integer> prime = new ArrayList<Integer>();
for (int i = 0; i < isprime.length; i++) {
isprime[i] = true;
}
for (int i = 2; i < isprime.length; i++) {
if (isprime[i]) {
prime.add(i);
for (int j = i * 2; j < isprime.length; j += i) {
isprime[j] = false;
}
}
}
Scanner s = new Scanner(new File(args[0]));
while (true) {
int n = s.nextInt();
if (n == 0) {
break;
}
int count = 0;
int sum = 0;
int min = 0, max = 0;
//processing
while (max < prime.size()) {
sum += prime.get(max++);
if (sum == n) {
count++;
}
while (sum > n) {
sum -= prime.get(min++);
if (sum == n) {
count++;
}
}
}
System.out.println(count);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment