Approximate test instructions: given [l,r] interval, find each prime in the interval
Data range:
1<=l< r<=2,147,483,647)
R-l <=1,000,000.
R value is too large, so can not directly sieve [0,r], to space and time optimization, using the interval sieve method, but also note that can not use int, because R and L are full int, the middle has a lot of detail processing will explode int, also note that 1 is not prime, so in the interval sieve to special sentence, is a wrong place
1160k16msc++1539b#include<cstdio> #include <iostream> #include <algorithm> #include <cstring >using namespace Std;const int maxn = 1E6+1E3; The interval to be sifted [l,r] length const int N = 50001;//guaranteed to be greater than (2^31-1) of the arithmetic square root bool Prime[maxn];bool seive[n];typedef long long ll;int l,r,len; void Seg_seive (ll l,ll R)//Interval sieve method {len=r-l+1; for (int i=0;i<len;i++) prime[i]=1; if (1-l>=0) prime[1-l]=0; Easy error because 1 is not a prime or composite, which is also an error-prone bug for the interval sieve (ll i=2; i*i<=r; i++) {if (Seive[i]) {for (ll J=max ( LL) 2, (l-1+i)/i) *i;j<=r;j+=i)//Second error point, J must be from greater than 1, since L may be less than I, but Seive[i] is a prime number. Prime[j-l]=false; }}}int Main () {for (int i=2;i<n;i++) seive[i]=1; for (int i=2;i*i<n;i++)//preprocessing if (Seive[i]) for (int j=2*i;j<n;j+=i) Seive[j]=false; while (~SCANF ("%d%d", &l,&r)) {seg_seive (l,r); int lmax,rmax,lmin,rmin; int mmax=-1,mmin= (1<<30); int t=-1; for (inti=0;i<len;i++) if (Prime[i]) {if (t>=0) {if (M MAX<I-T) Mmax=i-t,lmax=t+l,rmax=i+l; if (mmin>i-t) mmin=i-t,lmin=t+l,rmin=i+l; t=i; } else t=i; } if (mmax>0) printf ("%d,%d is closest,%d,%d is most distant.\n", Lmin,rmin,lmax,rmax); Else puts ("There is no adjacent primes."); } return 0;}
POJ 2689 Prime Distance (prime number interval sieve Method-Classic problem)