UVa1210 - Sum of Consecutive Prime Numbers(欧拉筛法即线性筛法)
2014-08-20 18:53
513 查看
Some positive integers can be represented by a sum of one or more consecutive prime numbers. How many such representations does a given positive integer have? For example, the integer 53 has two representations
5 + 7 + 11 + 13 + 17 and 53. The integer
41 has three representations 2 + 3 + 5 + 7 + 11 + 13,
11 + 13 + 17, and 41. The integer 3 has only one representation, which is 3. The integer 20 has no such representations. Note that summands must be consecutive prime numbers, so neither
7 + 13 nor 3 + 5 + 5 + 7 is a valid representation for the integer 20. Your mission is to write a program that reports the number of representations for the given positive integer.
inserted in the output.
用欧拉筛法(即线性筛法)求出10000内的素数,然后用数组f[i]表示从第1个素数到第i个素数的和,用两重循环判断f[i] - f[j]是否等于n
import java.io.FileInputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.OutputStreamWriter;
import java.io.StreamTokenizer;
import java.util.ArrayList;
public class Main implements Runnable
{
private static final boolean DEBUG = false;
private static final int N = 10001;
private BufferedReader cin;
private PrintWriter cout;
private StreamTokenizer tokenizer;
private boolean[] vis;
private ArrayList<Integer> vPrime = new ArrayList<Integer>();
private int[] f;
private int n;
private void preprocess()
{
vis = new boolean
;
vis[0] = vis[1] = true;
for (int i = 2; i < N; i++) {
if (!vis[i]) {
vPrime.add(i);
}
int size = vPrime.size();
for (int j = 0; j < size && i * vPrime.get(j) < N; j++) {
int num = vPrime.get(j);
vis[i * num] = true;
if (i % num == 0) break;
}
}
int size = vPrime.size();
f = new int[size + 1];
f[0] = 0;
for (int i = 0; i < size; i++) {
f[i + 1] = f[i] + vPrime.get(i);
}
}
private void init()
{
try {
if (DEBUG) {
cin = new BufferedReader(new InputStreamReader(
new FileInputStream("d:\\OJ\\uva_in.txt")));
} else {
cin = new BufferedReader(new InputStreamReader(System.in));
}
tokenizer = new StreamTokenizer(cin);
cout = new PrintWriter(new OutputStreamWriter(System.out));
preprocess();
} catch (Exception e) {
e.printStackTrace();
}
}
private String next()
{
try {
tokenizer.nextToken();
if (tokenizer.ttype == StreamTokenizer.TT_EOF) return null;
else if (tokenizer.ttype == StreamTokenizer.TT_NUMBER) {
return String.valueOf((int)tokenizer.nval);
} else return tokenizer.sval;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
private boolean input()
{
n = Integer.parseInt(next());
if (n == 0) return false;
return true;
}
private void solve()
{
int size = f.length;
int ans = 0;
for (int i = 0; i < size; i++) {
for (int j = i + 1; j < size; j++) {
if (f[j] - f[i] == n) ans++;
}
}
cout.println(ans);
cout.flush();
}
public void run()
{
init();
while (input()) {
solve();
}
}
public static void main(String[] args)
{
new Thread(new Main()).start();
}
}
5 + 7 + 11 + 13 + 17 and 53. The integer
41 has three representations 2 + 3 + 5 + 7 + 11 + 13,
11 + 13 + 17, and 41. The integer 3 has only one representation, which is 3. The integer 20 has no such representations. Note that summands must be consecutive prime numbers, so neither
7 + 13 nor 3 + 5 + 5 + 7 is a valid representation for the integer 20. Your mission is to write a program that reports the number of representations for the given positive integer.
Input
The input is a sequence of positive integers each in a separate line. The integers are between 2 and 10000, inclusive. The end of the input is indicated by a zero.Output
The output should be composed of lines each corresponding to an input line except the last zero. An output line includes the number of representations for the input integer as the sum of one or more consecutive prime numbers. No other characters should beinserted in the output.
Sample Input
2 3 17 41 20 666 12 53 0
Sample Output
1 1 2 3 0 0 1 2
用欧拉筛法(即线性筛法)求出10000内的素数,然后用数组f[i]表示从第1个素数到第i个素数的和,用两重循环判断f[i] - f[j]是否等于n
import java.io.FileInputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.OutputStreamWriter;
import java.io.StreamTokenizer;
import java.util.ArrayList;
public class Main implements Runnable
{
private static final boolean DEBUG = false;
private static final int N = 10001;
private BufferedReader cin;
private PrintWriter cout;
private StreamTokenizer tokenizer;
private boolean[] vis;
private ArrayList<Integer> vPrime = new ArrayList<Integer>();
private int[] f;
private int n;
private void preprocess()
{
vis = new boolean
;
vis[0] = vis[1] = true;
for (int i = 2; i < N; i++) {
if (!vis[i]) {
vPrime.add(i);
}
int size = vPrime.size();
for (int j = 0; j < size && i * vPrime.get(j) < N; j++) {
int num = vPrime.get(j);
vis[i * num] = true;
if (i % num == 0) break;
}
}
int size = vPrime.size();
f = new int[size + 1];
f[0] = 0;
for (int i = 0; i < size; i++) {
f[i + 1] = f[i] + vPrime.get(i);
}
}
private void init()
{
try {
if (DEBUG) {
cin = new BufferedReader(new InputStreamReader(
new FileInputStream("d:\\OJ\\uva_in.txt")));
} else {
cin = new BufferedReader(new InputStreamReader(System.in));
}
tokenizer = new StreamTokenizer(cin);
cout = new PrintWriter(new OutputStreamWriter(System.out));
preprocess();
} catch (Exception e) {
e.printStackTrace();
}
}
private String next()
{
try {
tokenizer.nextToken();
if (tokenizer.ttype == StreamTokenizer.TT_EOF) return null;
else if (tokenizer.ttype == StreamTokenizer.TT_NUMBER) {
return String.valueOf((int)tokenizer.nval);
} else return tokenizer.sval;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
private boolean input()
{
n = Integer.parseInt(next());
if (n == 0) return false;
return true;
}
private void solve()
{
int size = f.length;
int ans = 0;
for (int i = 0; i < size; i++) {
for (int j = i + 1; j < size; j++) {
if (f[j] - f[i] == n) ans++;
}
}
cout.println(ans);
cout.flush();
}
public void run()
{
init();
while (input()) {
solve();
}
}
public static void main(String[] args)
{
new Thread(new Main()).start();
}
}
相关文章推荐
- 习题10-6 UVA - 1210 Sum of Consecutive Prime Numbers 连续素数之和(滑动窗口)
- Uva-1210 Sum of Consecutive Prime Numbers
- Sum of Consecutive Prime Numbers - UVa 1210 水题
- UVa 1210 - Sum of Consecutive Prime Numbers
- UVa 1210 - Sum of Consecutive Prime Numbers
- UVA 1210 Sum of Consecutive Prime Numbers(数论)
- UVa1210 Sum of Consecutive Prime Numbers
- UVa1210 - Sum of Consecutive Prime Numbers
- UVa 1210 - Sum of Consecutive Prime Numbers
- UVa 1210 - Sum of Consecutive Prime Numbers
- UVALive3399 UVA1210 POJ2739 Sum of Consecutive Prime Numbers【素数筛选+尺取法】
- UVa 1210 - Sum of Consecutive Prime Numbers(素数+连续和)
- UVa 1210 - Sum of Consecutive Prime Numbers(滑动窗口)
- 【UVA】1210 - Sum of Consecutive Prime Numbers
- UVA 1210 Sum of Consecutive Prime Numbers
- UVA 1210(p343)----Sum of Consecutive Prime Numbers
- Sum of Consecutive Prime Numbers UVA - 1210 连续素数之和
- [UVa1210]Sum of Consecutive Prime Numbers(前缀和,打表)
- UVA - 1210 - Sum of Consecutive Prime Numbers
- UVA 1210 Sum of Consecutive Prime Numbers(素数打表)