Prime Distance
time limit per test 1 second memory limit per test 256 megabytes
The branch of mathematics called number theory is about properties of numbers. One of the areas that has captured the interest of number theoreticians for thousands of years is the question of primality. A prime number is a number that is has no proper factors (it is only evenly divisible by 1 and itself). The first prime numbers are 2,3,5,7 but they quickly become less frequent. One of the interesting questions is how dense they are in various ranges. Adjacent primes are two numbers that are both primes, but there are no other prime numbers between the adjacent primes. For example, 2,3 are the only adjacent primes that are also adjacent numbers.
Your program is given 2 numbers: L and U (1<=L< U<=2,147,483,647), and you are to find the two adjacent primes C1 and C2 (L<=C1< C2<=U) that are closest (i.e. C2-C1 is the minimum). If there are other pairs that are the same distance apart, use the first pair. You are also to find the two adjacent primes D1 and D2 (L<=D1< D2<=U) where D1 and D2 are as distant from each other as possible (again choosing the first pair if there is a tie).

传送门:POJ2689

Input

Each line of input will contain two positive integers, L and U, with L < U. The difference between L and U will not exceed 1,000,000.

Output

For each L and U, the output will either be the statement that there are no adjacent primes (because there are less than two primes between the two given numbers) or a line giving the two pairs of adjacent primes.

Sample Input

1
2
2 17
14 17

Sample Output

1
2
2,3 are closest, 7,11 are most distant.
There are no adjacent primes.

题解

二次素筛的例题 数组肯定开不下 那么先枚举 根号n下的素数 然后用这些素数 再删区间里的非素数 因为区间只有1e6 所以可行 注意会tle的一点 不要一个一个枚举素因子的倍数 要用除法先一步找到大于左端点的
## AC code:(不包含输入类)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import java.io.*;
import java.util.*;
public class Main {
static boolean []prime;
static boolean[]twiceprime;
static ArrayList<Integer>list=new ArrayList<Integer>();
public static void main(String[] args) {
FastScanner sc=new FastScanner();
PrintWriter pw=new PrintWriter(System.out);
makePrime();
for(int i=0;i<=50000;i++){
if(prime[i]){
//pw.println(i);
list.add(i);
}
}
while(sc.hasNext()){
int a=sc.nextInt();
int b=sc.nextInt();
//tepan1
makePrimeTwice(a,b);
int count=0;
ArrayList<Integer>temp=new ArrayList<Integer>();
for(int i=0;i<b-a+1;i++){
if(twiceprime[i]){
temp.add(i+a);
}
}
if(temp.size()<=1){
pw.println("There are no adjacent primes.");
}else{
int min=Integer.MAX_VALUE;
int max=-1;
int minl=0;
int minr=0;
int maxl=0;
int maxr=0;
for(int i=1;i<temp.size();i++){
int left=temp.get(i-1);
int right=temp.get(i);
if(right-left<min){
min=right-left;
minl=temp.get(i-1);
minr=temp.get(i);
}
if(right-left>max){
max=right-left;
maxl=temp.get(i-1);
maxr=temp.get(i);
}
}
pw.print(minl+","+minr+" are closest, ");
pw.println(maxl+","+maxr+" are most distant.");
}
pw.flush();
}
}

//yichu find first x
static void makePrimeTwice(int l,int r){
twiceprime=new boolean[r-l+1];
Arrays.fill(twiceprime, true);
int dx=r-l+1;
long k=0;
for(int i=0;i<list.size();i++){
long temp=list.get(i);
k=l/temp; //important
while(k*temp<l||k<=1) k++;
for(long j=k*temp;j<=r;j+=temp){
if(j>=l){
twiceprime[(int) (j-l)]=false;
}
}
}
if(l==1)twiceprime[0]=false;
}
static void makePrime(){
prime=new boolean[50050];
Arrays.fill(prime, true);
prime[0]=false;
prime[1]=false;
for(int i=2;i<=Math.sqrt(50000);i++){
if(prime[i]){
for(int j=i*i;j<=50000;j+=i){
prime[j]=false;
}
}
}
}
}