Classic application of POJ 2638 Embedding Method

Source: Internet
Author: User

I won't talk about this question for a long time. I first got a wrong question and thought that the maximum value range is 0 ~ 1000000; I handed in WA and read the question carefully. It turns out that I am so naive... the data is so big,
We found that it only needs to take the continuous 1000000 range value, but we know that the lower part of the 2,147,483,647 open, if it cannot be 0 ~ The division of the quality factor in 2,147,483,647 ^ (1/2), it must be
Prime Number ~ All the prime numbers in the 2,147,483,647 ^ (1/2) interval are filtered out, and then each prime factor is traversed ~ All values in the U range are deleted by multiples of the prime factor p.
Dis [] is marked, and the final discretization is also done... wa I have returned multiple times to note that 1 is not a prime number...
Code:
# Include <iostream>
# Include <cstdio>
Using namespace std;
# Define manx 500000
Bool s [manx];
Int p [manx], num, dis [manx * 2 + 50];
Void prime (){
For (int I = 0; I <manx; I ++)
S [I] = 0;
For (int I = 2; I * I <manx; I ++) {// filter prime numbers
If (! S [I]) {
For (int j = 2; j * I <manx; j ++)
S [I * j] = 1;
}
}
Num = 0;
For (int I = 2; I <manx; I ++)
If (! S [I]) p [num ++] = I;
}
Int main (){
Long a, B;
Prime ();
While (cin> a> B ){
For (int I = 0; I <B-a + 3; I ++)
Dis [I] = 0;
Long min = 1000001, max =-1, l1 = 0, r1 = 0, l2 = 0, r2 = 0;
For (int I = 0; I <num; I ++) {// Delete is a multiple of p [], that is, mark it
Long j = a/p [I];
If (j * p [I]> B) break;
If (a % p [I]) j ++;
If (j = 1) j ++; // a must be a prime number if only 1 can be obtained by division.
For (; j * p [I] <= B; j ++) {// delete a multiple of p [I]
Dis [j * p [I]-a] = 1;
}
}
Long g = 0, left;
For (long I = 0; I <= B-a; I ++ ){
If (! G &&! Dis [I]) {
Left = I;
If (left + a = 1) continue; // 1 is not a prime number. You must note that WA has returned many times.
G = 1; continue;
}
If (! Dis [I]) {
If (min> I-left ){
Min = I-left;
L1 = left; r1 = I;
}
If (max <I-left ){
Max = I-left;
L2 = left; r2 = I;
}
Left = I;
}
} Www.2cto.com
If (min = 1000001 & max =-1) printf ("There are no adjacent primes. \ n ");
Else printf ("% lld, % lld are closest, % lld, % lld are most distant. \ n ", l1 + a, r1 + a, l2 + a, r2 + );
}
}

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.