POJ 2689 Prime Distance (sieve a number of primes (or composite) within a range)

Source: Internet
Author: User

: "Test instructions Instructions"

Give you the specified range [L, U], in this range to find the adjacent nearest and farthest two sets of prime numbers, if the nearest or farthest value is the same, the output of the smaller group. Among them: 1≤l, another u-l≤1000000.

"Problem analysis"

This problem is related to prime numbers, obviously if you can find a prime number between [L, U], and then scan from the back to the desired result, but the question is that the range of L and U is too large, it is impossible to achieve within the specified time.

But here's another condition: u-l≤1000000, if we only ask for a prime number within 1000000, it can be achieved at the specified time! But since we are not asking for a prime number within 1~1000000, we need to do a bit of deformation:

Establish one by one correspondence with 0~1000000 and l~u (i.e. I-l, l≤i≤u).

use bool b[1000001] to indicate whether each position is a prime number. Using the filter method to find the prime number between l~u (the actual screening process is to remove composite, so every composite, B array corresponding position is marked composite).

Finally start scanning from the 1th prime number!

What needs to be reminded here is the case of l=1.

#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath >using namespace std; #define LL Long longconst int n=50000;int num,num1;ll Prime[n],prime1[80005];bool flag[1000005]    ;//First Sieve all primes within n to void Get_prime () {memset (flag,0,sizeof (flag));    num=0;        for (int i=2;i<n;i++) {if (!flag[i]) prime[++num]=i;            for (int j=1;j<=num&&prime[j]*i<n;j++) {flag[i*prime[j]]=1;        if (i%prime[j]==0) break;      }}}ll L,r;    Record the left and right end points//sieve the prime number within the [l,r] interval void get_prime1 () {memset (flag,0,sizeof (flag));        for (ll i=1;i<=num;i++) {if (prime[i]*prime[i]>r) break;  ll K=l%prime[i]==0?l: (L+prime[i]-l%prime[i]);     Find out the first number of >=l that divide the primes if (K==prime[i]) k+=prime[i];        Avoid sifting itself into non-prime for (ll J=k;j<=r;j+=prime[i]) {flag[j-l]=1;        }} if (L==1) flag[0]=1;    Special Award 1 Num1=0; for (ll i=l;i<=r;i++) if (!flag[i-l]) prime1[++num1]=i;} int main (){get_prime ();        while (~SCANF ("%lld%lld", &l,&r)) {int flag1=1;        Get_prime1 ();        if (num1<=1) flag1=0;        ll a1,b1,a2,b2,ma=0,mi=100000000;            for (int i=2;i<=num1;i++) {ll k=prime1[i]-prime1[i-1];            if (ma<k) ma=k,a1=prime1[i-1],b1=prime1[i];        if (mi>k) mi=k,a2=prime1[i-1],b2=prime1[i];        } if (!FLAG1) printf ("There is no adjacent primes.\n");        else{printf ("%lld,%lld is closest,%lld,%lld is most distant.\n", A2,B2,A1,B1); }} return 0;}


POJ 2689 Prime Distance (sieve a number of primes (or composite) within a range)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.