Interval sieve prime number

Topic links: https://cn.vjudge.net/contest/319720#overview

 

Title effect: two input numbers L and U (1 <= L <U <= 2 147 483 647), to find two neighboring prime numbers C1 and C2 (L <= C1 <C2 <= U) is the minimum distance if more than one pair of adjacent prime number, choose the original, but also to identify two adjacent primes D1, and D2 is the maximum distance (originally the same choice in how to circumstances)

Wherein the difference L <U, L and U of not more than 1 000 000

Sample input:

2 17
14 17

Sample output:

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

 

 

Ideas:

Need to obtain all of the prime numbers in a given range interval, and then the maximum distance between primes, find out the minimum distance. First, we have to use a screen sieve interval [L, U] prime number within the range.

Because the data section exceeds the upper bound hit 2100000000, not all primes less than 21 million survive, but we found that the length of the interval is not more than 1 000 000, the use of the sieve screen out [L, U] All non-prime section We need to know [L, U] interval all non-prime factor a prime number (as a non-prime number which is the smallest prime factor screened out). Is a prime number or a number within 2147483647, the number can be divided by the prime number 2147483647, i.e., [L, U] interval all prime factors of non-prime numbers are within the root 2147483647.

Can advance all prime numbers to find out the number 2147483647, then used to prime screened out all non-designated section primes

 

 1 #include<iostream>
 2 #include<vector>
 3 #include<cstdio>
 4 #include<cstring>
 5 #include<cmath>
 6 using namespace std;
 7 typedef long long ll;
 8 const int maxn=50005;
 9 int prime[maxn];
10 int l,u,tot=0;
11 bool isprime[1000005];
12 void getprime(int N){
13     memset(prime,0,sizeof(prime));
14     for(int i=2;i<=N;i++){
15         if(!prime[i])
16             prime[tot++]=i;
17         for(int j=0;j<tot&&prime[j]*i<=N;j++){
18             prime[i*prime[j]]=1;
19             if(i%prime[j]==0) break;
20         }
21     }
22 }
23 intmain () {
 24      // The freopen ( "../ in.txt", "R & lt", stdin); 
25      getprime ( 50002 );
 26 is      the while (CIN >> L >> U) {
 27          Memset (isPrime, to true , the sizeof (isPrime));
 28          for ( int I = 0 ; I <TOT; I ++ ) {
 29              int A = (L- . 1 ) / Prime [I] + . 1 , B = U / Prime [I]; // L and r represents current in the range of maximum and minimum number multiple of the multiples of prime numbers. Why write, may themselves be understood that for example 
30              for ( int J = A; J <= B; J ++) IF (J> . 1) IsPrime [Prime * J [I] -l] = 0 ; // if j = 1, when this time is prime [i], prime [i ] is the prime number 
31 is          }
 32          IF (L == . 1 ) isPrime [ 0 ] = 0 ; // Note Laid arbitration case l = 1, 
33 is          int POS = - 1 , L1, R1, L2, R2, maxdis = 0 , = mindis 0x3f3f3f3f ;
 34 is          for ( int I = 0 ; I <= UL; ++ I ) {
 35              IF (isPrime [I]) {
 36                  IF (POS == - . 1 ) {
 37 [                      POS = I;
38                     continue;
39                 }
40                 if(i-pos<mindis){
41                     l1=l+pos;
42                     r1=l+i;
43                     mindis=i-pos;
44                 }
45                 if(i-pos>maxdis){
46                     maxdis=i-pos;
47                     l2=l+pos;
48                     r2=l+i;
49                 }
50                 pos=i;
51             }
52         }
53         if(maxdis==0) printf("There are no adjacent primes.\n");
54         else printf("%d,%d are closest, %d,%d are most distant.\n",l1,r1,l2,r2);
55     }
56     return 0;
57 }

 

Guess you like

Origin www.cnblogs.com/-Ackerman/p/11348370.html