! POJ 2689 Prime distance-card Time-(prime sieve method)

Source: Internet
Author: User

Test instructions: Given two number l,r, this is the nearest and furthest two primes between the two. The data range is the upper bound of integers. R-l<=10^6

Analysis: The general idea is to find the prime number between L and R, and then iterate over the minimum distance and the maximum distance. It is unrealistic to preprocess all the primes in the data range with a function, where the array cannot be opened so large and then times out. Think of the idea of prime sieve: Use the prime number within sqrt (n) to sieve out all composite within N, leaving the prime number within N. Since the pretreatment is not possible, we can use the same number of sqrt (n) to sieve, just at this time the prime number pretreatment, and then each input L and r when the prime number of pre-processing to sift off the composite between L and R, the remaining prime. SQRT (n) is 2^16, which means that 65536 does not time out.

The key is the prime sieve between the l,r. or use vis[] arrays to do this. Using two loops to traverse L~r and prim[] estimates that it should be timed out, so using a bit of optimization technique is to traverse l~r each time add prim[i], instead of adding 1

Code:

 #include <iostream> #include <cstring>using namespace Std;long long l,r,vis[ 5000003],l1,l2,r1,r2,init;int prim[500005],mi,mx,cnt;void Is_prim () {memset (vis,1,sizeof (Vis)); Cnt=0;for (int i=2;i <=50000;i++) {if (Vis[i]) {prim[cnt++]=i;for (int j=2;j*i<=50000;j++) vis[j*i]=0;}}} int main () {Is_prim (); while (Cin>>l>>r) {for (int i=0;i<r-l+1;i++) vis[i]=1;if (l==1) vis[0]=0;for (int i= 0;i<cnt&&prim[i]*prim[i]<=r;i++) {for (Long Long) max (Long Long) (prim[i]<<1), (L +    prim[i]-1)/prim[i]*prim[i]); j<=r;j+=prim[i]) vis[j-l]=0;} init=-1;mx=-1,mi=10000000;for (int i=0;i<r-l+1;i++) {if (Vis[i]) {if (init!=-1) {if (mi>i-init) {mi=i-init;l1= Init+l;r1=i+l;} if (mx<i-init) {mx=i-init;l2=init+l;r2=i+l;}} init=i;}} if (mx==-1) cout<< "There is no adjacent primes." <<endl;else cout<<l1<< "," <<r1<< "is closest," <<l2<< "," <<r2< < "is most distant." <<endl;}} 


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

! POJ 2689 Prime distance-card Time-(prime sieve method)

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.