Prime Distance

Posted hbhdhd

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Prime Distance相关的知识,希望对你有一定的参考价值。

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.
  
题意:给定一个区间,找去这个区间里相邻的最近的和最远的两组素数。
思路:根据题目中的数据范围,可以看出要用到埃式筛法,直接暴力肯定是不行的,然后直接无脑写就行了,好吧这就是我调了两天才AC的原因,我用的是挑战程序设计竞赛的模板。。。。。。阿西吧。
一直过不了,不废话了,具体看代码中的注释吧。
技术图片
  1 #include <map>
  2 #include <set>
  3 #include <list>
  4 #include <stack>
  5 #include <queue>
  6 #include <deque>
  7 #include <cmath>
  8 #include <ctime>
  9 #include <string>
 10 #include <limits>
 11 #include <cstdio>
 12 #include <vector>
 13 #include <iomanip>
 14 #include <cstdlib>
 15 #include <cstring>
 16 #include <istream>
 17 #include <iostream>
 18 #include <algorithm>
 19 #define ci cin
 20 #define co cout
 21 #define el endl
 22 #define Scc(c) scanf("%c",&c)
 23 #define Scs(s) scanf("%s",s)
 24 #define Sci(x) scanf("%d",&x)
 25 #define Sci2(x, y) scanf("%d%d",&x,&y)
 26 #define Sci3(x, y, z) scanf("%d%d%d",&x,&y,&z)
 27 #define Scl(x) scanf("%I64d",&x)
 28 #define Scl2(x, y) scanf("%I64d%I64d",&x,&y)
 29 #define Scl3(x, y, z) scanf("%I64d%I64d%I64d",&x,&y,&z)
 30 #define Pri(x) printf("%d
",x)
 31 #define Prl(x) printf("%I64d
",x)
 32 #define Prc(c) printf("%c
",c)
 33 #define Prs(s) printf("%s
",s)
 34 #define For(i,x,y) for(int i=x;i<y;i++)
 35 #define For_(i,x,y) for(int i=x;i<=y;i++)
 36 #define FFor(i,x,y) for(int i=x;i>y;i--)
 37 #define FFor_(i,x,y) for(int i=x;i>=y;i--)
 38 #define Mem(f, x) memset(f,x,sizeof(f))
 39 #define LL long long
 40 #define ULL unsigned long long
 41 #define MAXSIZE 1000005
 42 #define INF 0x3f3f3f3f
 43 
 44 const int mod=1e9+7;
 45 const double PI = acos(-1.0);
 46 
 47 using namespace std;
 48 
 49 bool is_prime[MAXSIZE];
 50 bool is_prime_small[MAXSIZE];
 51 //is_prime[i-a]=true--->i是素数
 52 void solve(LL a,LL b)
 53 {
 54     for(int i=0; (LL)i*i<=b; i++)
 55         is_prime_small[i]=true;
 56     //is_prime_small[0]=is_prime_small[1]=false;
 57     for(int i=0; i<=b-a; i++)
 58         is_prime[i]=true;
 59     if(a==1)
 60         is_prime[0]=0;//就是这个特判,我调了一天才搞出来 。。。。。。
 61     for(int i=2; (LL)i*i<=b; i++)
 62         if(is_prime_small[i])
 63         {
 64             for(int j=2*i; (LL)j*j<=b; j+=i)
 65                 is_prime_small[j]=false;//筛2~根号b
 66             for(LL j=max(2LL,(a+i-1)/i)*i; j<=b; j+=i)
 67                 is_prime[j-a]=false;//筛a~b
 68         }
 69 }
 70 //j = (ll)(a-1+i)/i*i
 71 //(a-1+i)/i*i是对a/i向上取整,此计算的作用是求得第一个>=a的i的倍数。
 72 //for(LL j=max(2LL,(a+i-1)/i)*i; j<=b; j+=i)这个循环的初始条件还是不懂
 73 
 74 int main()
 75 {
 76     LL a,b;
 77     LL c1,c2,d1,d2;
 78     while(~Scl2(a,b))
 79     {
 80         solve(a,b);
 81         int tmp=-1;
 82         int minn=INF,maxx=-1;
 83         queue<int>q1,q2;
 84         For_(i,a,b)
 85         {
 86             if(is_prime[i-a])
 87             {
 88                 if(tmp!=-1)
 89                 {
 90                     if(i-tmp<minn)
 91                     {
 92                         minn=i-tmp;
 93                         c1=tmp;
 94                         c2=i;
 95                     }
 96                     if(i-tmp>maxx)
 97                     {
 98                         maxx=i-tmp;
 99                         d1=tmp;
100                         d2=i;
101                     }
102                 }
103                 tmp=i;
104             }
105         }
106         if(minn!=INF||maxx!=-1)
107             cout<<c1<<","<<c2<<" are closest, "<<d1<<","<<d2<<" are most distant."<<endl;
108         else
109             Prs("There are no adjacent primes.");
110     }
111     return 0;
112 }
View Code

以上是关于Prime Distance的主要内容,如果未能解决你的问题,请参考以下文章

Prime Distance(POJ 2689)

Prime Distance(二次筛素数)

poj - 2689 Prime Distance

Prime Distance

Prime Distance

poj_2689_Prime Distance