Prime number Sieve nbut1454 Lux

Source: Internet
Author: User

Portal: Click to open link

Test instructions: Gives the interval [l,r],r-l<=1e6, the maximum and minimum of the difference between the adjacent prime numbers in the interval, and if there is no two prime number, the output-1

Idea: Sift through all prime numbers in the [L,r] interval, and then iterate over

#include <map> #include <set> #include <cmath> #include <stack> #include <queue> #include <cstdio> #include <string> #include <vector> #include <cstring> #include <iostream># include<algorithm> #include <functional> #define FIN freopen ("Input.txt", "R", stdin) #define FOUT freopen (" Output.txt "," w+ ", stdout) using namespace Std;typedef long Long ll;typedef pair<int, int>pii;const int MX = 1e6 + 5;co NST int INF = 0x3f3f3f3f;bool sign[mx];int subprim[mx], prim[mx], TOTLEPRM = 0;void Getprim (int n) {memset (sign,0,size    of (sign));    SIGN[1] = 1;        for (int i = 2; I <= n; i++) {if (sign[i]) continue;        prim[totleprm++] = i;        if (LL) i * i > N) continue;        for (int j = i * i; j <= N; j + = i) {sign[j] = 1; }}}int Getsubprim (int a, int b) {int totl = 0, I;    LL J;    Memset (sign, true, sizeof);    if (a < 2) a = 2;    LL L = b-a + 1; for (i = 0; i < TOTLEPRM; i++) {iF ((j = prim[i] * (a/prim[i)) < a) J + = Prim[i];        if (J < prim[i]*prim[i]) j = prim[i] * Prim[i];    for (; J <= B; j + = Prim[i]) sign[j-a] = false;        } for (i = 0; i < L; i++) if (Sign[i]) {subprim[totl++] = a + i; } return totl;}    int main () {Getprim (100000);    int L, R;//fin;        while (~SCANF ("%d%d", &l, &r)) {int minl = 0, MinR = INF, MaxL = 0, MaxR = 0;        int rear = Getsubprim (L, R);            if (Rear < 2) {printf (" -1\n");        Continue } for (int i = 0; i < rear-1; i++) {if (Minr-minl > subprim[i + 1]-subprim[i]) Minl = Subprim            [i], MinR = subprim[i + 1];        if (Maxr-maxl < Subprim[i + 1]-subprim[i]) MaxL = Subprim[i], MaxR = subprim[i + 1];    } printf ("%d%d%d%d\n", Minl, MinR, MaxL, MaxR); } return 0;}


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

Prime number Sieve nbut1454 Lux

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.