Created
January 29, 2014 10:28
-
-
Save kunishi/8685316 to your computer and use it in GitHub Desktop.
ICPC 2005, Tokyo Region, Problem A
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package jp.ac.oka_pu.icpc2005tokyo; | |
import java.io.BufferedReader; | |
import java.io.FileNotFoundException; | |
import java.io.FileReader; | |
import java.io.IOException; | |
public class A { | |
class Prime { | |
public int n; | |
public int sum; | |
public Prime(int n, int sum) { | |
this.n = n; | |
this.sum = sum; | |
} | |
} | |
Prime[] primes; | |
public A() { | |
boolean isPrime[] = new boolean[10001]; | |
int primeNumber = 0; | |
for (int i = 2; i < 10001; i ++) { | |
isPrime[i] = true; | |
} | |
for (int i = 2; i < 10001; i ++) { | |
if (isPrime[i]) { | |
primeNumber ++; | |
int k = 2; | |
while (i * k <= 10000) { | |
isPrime[i * k] = false; | |
k ++; | |
} | |
} | |
} | |
primes = new Prime[primeNumber]; | |
int sum = 0; | |
for (int i = 2, j = 0; i < 10001; i ++) { | |
if (isPrime[i]) { | |
sum += i; | |
primes[j ++] = new Prime(i, sum); | |
} | |
} | |
} | |
/** | |
* @param args | |
*/ | |
public static void main(String[] args) { | |
A a = new A(); | |
a.calc(args[0]); | |
} | |
public void calc(String file) { | |
BufferedReader br; | |
int n; | |
try { | |
br = new BufferedReader(new FileReader(file)); | |
n = Integer.parseInt(br.readLine()); | |
while (n != 0) { | |
System.out.println(countSums(n)); | |
n = Integer.parseInt(br.readLine()); | |
}; | |
br.close(); | |
} catch (FileNotFoundException e) { | |
e.printStackTrace(); | |
} catch (NumberFormatException e) { | |
e.printStackTrace(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
public int countSums(int n) { | |
int count = 0; | |
for (int i = primes.length - 1; i >= 0 && primes[i].sum >= n; i --) { | |
if (primes[i].sum == n) { | |
count ++; | |
} else { | |
int difference = primes[i].sum - n; | |
for (int j = 0; primes[j].sum <= difference; j ++) { | |
if (primes[j].sum == difference) { | |
count ++; | |
} | |
} | |
} | |
} | |
return count; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 | |
3 | |
17 | |
41 | |
20 | |
666 | |
12 | |
53 | |
0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment