UVa11408 - Count DePrimes(线性筛法)

Count DePrimes 

A number is called a DePrime if the sum of its prime factors is a prime number.

Given a and b count the number of DePrimesxi such that a$ \le$xi$ \le$b.

Input 

Each line contains a and b in the format ``a b". 2$ \le$a$ \le$5000000.a$ \le$b$ \le$5000000.

Input is terminated by a line containing `0'.

Output 

Each line should contain the number of DePrimes for the corresponding test case.


Explanation:

In Test Case 2, take 10. Its Prime Factors are 2 and 5. Sum of Prime Factors is 7, which is a prime. So, 10 is a DePrime.

Sample Input 

2 5
10 21
100 120
0

Sample Output 

4
9
9
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
import java.util.Arrays;
import java.util.ArrayList;

public class Main {
	public static final boolean DEBUG = false;
	public static final int N = 5000001;
	public StreamTokenizer tokenizer;
	public BufferedReader cin;
	public PrintWriter cout;
	public int a, b;
	public boolean[] vis = new boolean[N];
	public ArrayList<Integer> vPrime = new ArrayList<Integer>();
	public int[] f = new int[N];
	public int[] sum = new int[N];
	
	public 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));
			}
			cout = new PrintWriter(new OutputStreamWriter(System.out));
			tokenizer = new StreamTokenizer(cin);
			preprocess();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public String next() 
	{
		try {
			
			tokenizer.nextToken();
			if (tokenizer.ttype == StreamTokenizer.TT_EOF) return null;
			else if (tokenizer.ttype == StreamTokenizer.TT_WORD) return tokenizer.sval; 
			else if (tokenizer.ttype == StreamTokenizer.TT_NUMBER) return String.valueOf((int)tokenizer.nval);
			else return null;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public boolean input()
	{
		a = Integer.parseInt(next());
		if (a == 0) return false;
		
		b = Integer.parseInt(next());
		return true;
	}	
	
	
	public void preprocess()
	{
		Arrays.fill(vis, false);
		Arrays.fill(sum, 0);
		
		vis[0] = vis[1] = true;
		for (int i = 2; i < N; i++) {
			if (!vis[i]) {
				vPrime.add(i);
				sum[i] = i;
			}
			
			for (int j = 0, size = vPrime.size(); j < size && i * vPrime.get(j) < N; j++) {
				int tmp = vPrime.get(j);
				vis[i * tmp] = true;
				if (i % tmp == 0) {
					sum[i * tmp] = sum[i];
					break;
				} else {
					sum[i * tmp] = sum[i] + tmp;
				}
			}
		}
		
		f[0] = f[1] = 0;
		for (int i = 2; i < N; i++) {
			f[i] = f[i - 1] + (vis[sum[i]] == false ? 1 : 0);
		}
	}
	
	
	
	public void solve() 
	{
		int cnt = f[b] - f[a - 1];
		
		cout.println(cnt);
		cout.flush();
	}

	public static void main(String[] args) {
		Main solver = new Main();
		solver.init();
		
		while (solver.input()) {
			solver.solve();
		}
	}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kgduu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值