POJ 2689 Prime Distance

Prime Distance
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 9195   Accepted: 2490

Description

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).

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

2 17
14 17

Sample Output

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


绝对经典的素数筛选题,本人历经TLE,RE,WA,终于取得AC,修成正果
个人感觉这个题对算法复杂度要求还是比较高的,毕竟数据量很大,所以筛的时候一定要慎之又慎
首先用initial_1()函数筛出sqrt(2^31-1)中所有的素数储存起来,然后每次读入一个区间,用initial_2()筛出所求区间中所有的素数,最后在main()函数中查找出最近素数和最远素数即可
这里面最为关键的在于initial_2()的实现,从别人博客抄了一行极为经典的代码,在注释中有标注

[C++]
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cmath>
 4 #include<cstring>
 5 #include<vector>
 6 
 7 using namespace std;
 8 
 9 vector<long> smallPrime;
10 bool largePrime[1000001];
11 
12 void initial_1()
13 {
14     bool isPrime[46342];
15 
16     memset(isPrime,true,sizeof(isPrime));
17     isPrime[0]=isPrime[1]=false;
18     for(long i=4;i<=46341;i+=2)
19         isPrime[i]=false;
20     for(long i=3;i<216;i+=2)
21         if(isPrime[i])
22             for(int j=i*2;j<=46341;j+=i)
23                 isPrime[j]=false;
24     for(long i=2;i<=46341;i++)
25         if(isPrime[i])
26             smallPrime.push_back(i);
27 }
28 
29 void initial_2(long a,long b)
30 {
31     memset(largePrime,true,sizeof(largePrime));
32     if(a==1)
33         largePrime[0]=false;
34     for(long i=0;i<smallPrime.size()&&smallPrime[i]<=(long)sqrt((double)b);i++)
35     {
36         long long begin=(a/smallPrime[i])+(a%smallPrime[i]>0);  //应该说这行代码是整题的精髓,直接将筛选范围直接定位到所求的区间内
37         if(begin==1)
38             begin++;
39         for(long long j=begin*smallPrime[i];j<=b;j+=smallPrime[i])
40             largePrime[j-a]=false;
41     }
42 }
43 
44 int main()
45 {
46     long a,b;
47 
48     initial_1();
49 
50     while(cin>>a>>b)
51     {
52         bool flag=false;
53         long min=1000000,max=0,minl,minr,maxl,maxr,last=-1;
54         initial_2(a,b);
55         for(long long i=a;i<=b;i++)
56         {
57             if(largePrime[i-a])
58             {
59                 if(last!=-1)
60                 {
61                     long temp=i-last;
62                     flag=true;
63                     if(temp>max)
64                     {
65                         max=temp;
66                         maxl=last;
67                         maxr=i;
68                     }
69                     if(temp<min)
70                     {
71                         min=temp;
72                         minl=last;
73                         minr=i;
74                     }
75                 }
76                 last=i;
77             }
78         }
79         if(flag)
80             printf("%ld,%ld are closest, %ld,%ld are most distant.\n",minl,minr,maxl,maxr);
81         else
82             printf("There are no adjacent primes.\n");
83     }
84 
85     return 0;
86 }

 

posted @ 2013-05-12 20:48  ~~Snail~~  阅读(600)  评论(0编辑  收藏  举报