When working on a topic, reading in different format characters will have different results on different OJ.
poj_2689/uva_10140 Prime Distance
POJ Topic Link: http://poj.org/problem?id=2689
Title:
Description
The branch of mathematics called Number theory is about properties of numbers. One of the areas that's have captured the interest of number theoreticians for thousands of years's the question of Primalit Y. A prime number is a number and this is have has no proper factors (it's only evenly divisible by 1 and itself). The first prime numbers is 2,3,5,7 but they quickly become less frequent. One of the interesting questions is what dense they is in various ranges. Adjacent primes is and numbers that is both primes, but there is no other prime numbers between the adjacent primes. For example, 2,3 is the only adjacent primes that is also adjacent numbers.
Your program was given 2 numbers:l and U (1<=l< u<=2,147,483,647), and you were to find the both adjacent primes C1 and C2 (l<=c1< C2<=u) that's closest (i.e. C2-C1 is the minimum). If There is other pairs that is the same distance apart, use the first pair. You is also to find the adjacent primes D1 and D2 (l<=d1< d2<=u) where D1 and D2 is as distant from each OT Her as possible (again choosing, the first pair if there is a tie).
Input
Each line of input would contain the positive integers, L and u, with L < U. The difference between L and U would not exceed 1,000,000.
Output
For each L and U, the output would either is the statement that there is no adjacent primes (because there is less than t Wo primes between the both given numbers) or a line giving the pairs of adjacent primes.
Sample Input
2 17
14 17
Sample Output
2,3 is closest, 7,11 is most distant.
There is no adjacent primes.
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 ();" "**//while (scanf ("%d%d", &left,&r) ==2)//This is written on the POJ will tle, can be AC on UVA while(scanf("%lld%lld", &left,&r) = =2)//This can be ac** on both POJ and UVA " "{memsetA0,sizeof(a));if(left==1) {a[0]=1; } for(intI=0; i<num_p && prime[i]*prime[i]<=r;i++) {intK= (int) Left/prime[i]; LL M=k*prime[i]; while(M<left | | k<=1) {m+= (LL) prime[i]; k++; } for(LL J=m;j<=r;j+=prime[i]) {if(J>=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) {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;}
Read into scanf,%d and%LLD questions