Clever Use of poj 2689 prime number Filtering

Source: Internet
Author: User

Question:

Returns an interval [L, R] to obtain the shortest prime number and the longest distance in the interval. (R <2*10 ^ 9, R-l <= 10 ^ 6)

The number of factors allowed by number theory can be obtained within the root number. Therefore, we can obtain the prime number in 5*10 ^ 4. Then, we use a screening method to find the sum of numbers in [L, R], and the rest is the prime number. Discretization is used here to save a number X-l in the array. Because it cannot be saved directly, but we find that the interval has small characteristics. Therefore, we can think of discretization.

 

# Include <iostream> # include <algorithm> # include <cstdio> # include <cstring> # include <cmath> using namespace STD; typedef long ll; const int maxn = 50000; int primes [maxn]; bool VST [maxn]; int notprimes [1000010]; int POS [1000010]; int top, PCNT; void Init () {Top = 0; memset (VST, 0, sizeof (VST); VST [0] = VST [1] = 1; for (INT I = 2; I <maxn; ++ I) if (! VST [I]) {primes [top ++] = I; for (Int J = I + I; j <maxn; j + = I) VST [J] = 1;} // printf ("Top: % d \ n", top);} void solve (int l, int R) {memset (notprimes, 0, sizeof (notprimes); If (L = 1) L = 2; // prevent all prime numbers in this interval from being screened !!!!! For (INT I = 0; I <top & (LL) primes [I] * primes [I] <= r; ++ I) {// filter factor int S = L/primes [I] + (L % primes [I]> 0 ); // The minimum multiple of the current prime number reaches l s = (S = 1? 2: S); // prevents all prime numbers in this interval from being screened !!!!! For (Int J = s; (LL) J * primes [I] <= r; ++ J) {If (LL) J * primes [I]> = L) // sum notprimes [J * primes [I]-l] = 1; // equivalent to discretization} PCNT = 0; For (INT I = 0; I <= R-l; ++ I) {If (! Notprimes [I]) {pos [PCNT ++] = I + L; // printf ("I --> % d \ n", I + l );}} if (PCNT <2) {puts ("there are no adjacent primes. ");} else {int minl, minr, maxl, maxr, MINV = 999999, maxv =-1; for (INT I = 1; I <PCNT; ++ I) {If (Pos [I]-pos [I-1]> maxv) {maxv = POS [I]-pos [I-1]; maxl = POS [I-1]; maxr = POS [I];} If (Pos [I]-pos [I-1] <MINV) {MINV = POS [I]-pos [I-1]; minl = POS [I-1]; minr = POS [I];} printf (" % D, % d are closest, % d, % d are most distant. \ n ", minl, minr, maxl, maxr) ;}} int main () {Init (); int L, R; while (~ Scanf ("% d", & L, & R) {solve (L, R);} return 0 ;}


 

Clever Use of poj 2689 prime number Filtering

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.