UVA Topic link:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&itemid=35&page=show_problem& category=13&problem=1081&mosmsg=submission+received+with+id+15689014
The main problem: in the range L and R, to find the difference between the L and R range of the smallest and largest two pairs. 1=< l<=r<=2,147,483,647,r-l<=1000000
Analysis:
The number of squares, the classic question. Because the largest is 2^31-1, the root number is less than 46341, so you can do the first time the number of screening selected, screening out the 1~50000 of the number, and then use 1~50000 to screening to select any L to r between the two. With a a[], the number of a[l]~a[r in the space of the desired area, and the number of the number of things that are required to a[0]~a[r-l].
Password:
#include <cstdio>#include <cstdlib>#include <cstring>using namespace STD;#define RANGE 50000#define RANGE2 1000010#define INF 0xFFFFFFFtypedef Long LongLL;intPrime[range],is_prime[range];intA[RANGE2],PRIME2[RANGE2];intnum_p;voidProduce_prime () {memset(Is_prime,0,sizeof(Is_prime)); num_p=0; for(intI=2; i<range;i++) {if(is_prime[i]==0) {intJ=i+i; Prime[num_p++]=i; while(J<range) {is_prime[j]=1; J+=i; } } }}intMain () {//Freopen ("In.txt", "R", stdin);LL Left,r; Produce_prime ();//Pre-screening out 50000 of the content while(scanf("%lld%lld", &left,&r) = =2)//Here, if used with%d%d, will be tle on POJ, but can be AC on UVA{memsetA0,sizeof(a));if(left==1) {a[0]=1;//If l=1, it can not be counted, this is a special contract} for(intI=0; i<num_p && prime[i]*prime[i]<=r;i++)//Starting with the smallest number, the loop is smaller than the number of fractions and the open root of R{intK= (int) Left/prime[i];///from prime[i] screening, and this is more than a few times as much as left,k for Prime[i]LL M=k*prime[i]; while(M<left | | k<=1)//Because k is divisible, so if left is not prime[i] the whole number of times, the remainder of the part will be removed, or appear left<prime[i] situation; There is just left is the element, k=1, this time left can not be screening off, so let K >1 and M>=k .{m+= (LL) prime[i]; k++; } for(LL J=m;j<=r;j+=prime[i]) {if(J>=left)//Start with the number of screening, and screening the multiples of prime[i] in Left~r, because left,r can be very large, this uses a * * offset index** technique, that is a[j]=1, etc. at a[j-left]=1, screening all A[left]~a[r] ==a[0]~a[r-left]a[j-left]=1; } }intminn=inf,maxx=-1;intminflag=-1, minf=-1, minlast=-1, maxf=-1, maxlast=-1, pre=-1; for(LL i=left;i<=r;i++) {if(a[i-left]==0)///This is also used to offset the index to determine whether it was screening out{if(pre==-1) {pre=i; }Else{if(I-pre<minn) {minf=pre; minlast=i; Minn=i-pre; }if(I-pre>maxx) {maxf=pre; maxlast=i; Maxx=i-pre; } pre=i; }//last=i;} }if(minf==-1)printf("There is no adjacent primes.\n");Else printf("%d,%d is closest,%d,%d is most distant.\n", minf,minlast,maxf,maxlast); }return 0;}
Some important conclusions about the mass:
The PI (n) is smaller than the mass of N, and Pi (n) \approxn/n/ln (n)
When n is around 10^5 to 10^6, the number of masses can be estimated by 10/n.
The nth mass is approximately NLN (n)
Computer algorithm guidance: Design and analysis This part of the mass
Another way to find large numbers of masses:
http://www.jiancool.com/article/50042575894/
uva_10140/poj_2689 Prime Distance (Zone)