UVa967 - Circular(线性法筛素数)

A circular prime is a prime number that remains prime as each leftmost digit (most signi cant digit),
in turn, is moved to the right hand side. For instance, the number 19937 is a circular prime, since all
numbers in the sequence 19937, 99371, 93719, 37199 and 71993 are prime numbers. Your objective is
to write a program that, given a range, computes the number of circular primes in that range.
Input
The input consists of a sequence of pairs of integers
i
and
j
, with one pair of integers per input line.
All integers will be less than 1,000,000 and greater or equal to 100. You can assume that in any pair
i
is lesser or equal than
j
. You should process all pairs of integers, and for each such pair, count the
number of circular primes between
i
and
j
, including
i
and
j
. Input is terminated by a line just with
the number `
-1
'.
Output
For each pair of input integers, de ning a range, the output should be: `
No Circular Primes.
' (if
there are no circular primes in the range), `
1 Circular Prime.
' (if only one circular prime exists in
the range), or `
n
Circular Primes.
' (if there are
n
circular primes in the range, and
n
is greater than
one).
SampleInput
1000 1100
100 120
100 1000
-1
SampleOutput
No Circular Primes.
1 Circular Prime.
12 Circular Primes.

用线性筛法求出1-1000000之间的素数,同时用f(x)表示从1到x之间满足条件的数的个数,其输出结果相当于f(b) - f(a - 1)

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.ArrayList;

public class Main 
{
	private static final boolean DEBUG = false;
	private static final int N = 1000001;
	private BufferedReader cin;
	private PrintWriter cout;
	private StreamTokenizer tokenizer;
	private int a, b;
	private boolean[] vis;
	private ArrayList<Integer> vPrime = new ArrayList<Integer>();
	private int[] f;
	
	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_NUMBER) 
				return String.valueOf((int)tokenizer.nval);
			else return tokenizer.sval;
			
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}	
	}
	
	public boolean input() 
	{	
		a = Integer.parseInt(next());
		if (a == -1) return false;
		
		b = Integer.parseInt(next());
		return true;
	}
	
	private void preprocess()
	{
		vis = new boolean[N];
		vis[0] = vis[1] = true;
		
		for (int i = 2; i < N; i++) {
			if (!vis[i]) {
				vPrime.add(i);
			}
			
			for (int j = 0, size = vPrime.size(); j < size && vPrime.get(j) * i < N; j++) {
				int tmp = vPrime.get(j);
				vis[i * tmp] = true;
				if (i % tmp == 0) break;
			}
		}
		
		f = new int[N];
		f[0] = f[1] = 0;
		for (int i = 2; i < N; i++) {
			if (check(i)) {
				f[i] = f[i - 1] + 1;
			} else f[i] = f[i - 1];
		}
	}
	
	private boolean check(int num)
	{
		String s = String.valueOf(num);
		int len = s.length();
		
		for (int i = 0; i < len; i++) {
			StringBuilder sb = new StringBuilder();
			sb.append(s.substring(i));
			sb.append(s.subSequence(0, i));
			int n = Integer.parseInt(sb.toString());
			if (vis[n]) return false;
		}
		
		return true;
	}
	
	
	public void solve() 
	{
		int ans = f[b] - f[a - 1];
		
		if (ans == 0) cout.println("No Circular Primes.");
		else if (ans == 1) cout.println("1 Circular Prime.");
		else cout.println(ans + " Circular Primes.");
		cout.flush();
	}

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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

kgduu

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

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

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

打赏作者

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

抵扣说明:

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

余额充值