UVa967 - Circular(线性法筛素数)

来源:互联网 发布:java餐厅订餐系统设计 编辑:程序博客网 时间:2024/05/21 14:51
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