http://poj.org/problem?id=2689
/* Interval Sieve method for L, R range is larger, but the interval length is small, set a template and then remove the smallest maximum can be *//************************************************* Author:P owatr* Cr eated time:2015-8-17 9:38:50* File Name:P OJ2689.cpp ************************************************/#include <c stdio> #include <algorithm> #include <iostream> #include <sstream> #include <cstring># Include <cmath> #include <string> #include <vector> #include <queue> #include <deque># Include <stack> #include <list> #include <map> #include <set> #include <bitset> #include <cstdlib> #include <ctime>using namespace std; #define Lson L, Mid, RT << 1#define Rson mid + 1, R, RT < ;< 1 | 1typedef long ll;const int MAXN = 1e6 + 1e4;const int INF = 0x3f3f3f3f;const int MOD = 1e9 + 7;const int N = 50010;bo OL prime[maxn];bool Sieve[n];int len;void segent_sieve (ll L, ll R) {len = r-l + 1; for (int i = 0; i < len; i++) Prime[i] = true; if (1-l >= 0) prime[1-l] = false;//Special Award for (ll i = 2; i*i < R; i++) {if (Seive[i]) {for (ll j = Max (1ll*2, (l-1+i)/i) *i; j <= R; j+=i)//(l-1+i/i) Gets the multiple of I closest to L prime[j-l] = false; Offset, because the simple J will cause the array to go out of bounds, to get the range 0 to Len}}}int main () {///-NLOGNLOGN) for (int i = 2; i < N; i++) Sieve[i] = true; for (int i = 2; i*i < n, i++) {if (Sieve[i]) {for (int j = i*2; j < n; j+=i) {sieve[j] = false; }}} int L, R; while (~SCANF ("%d%d", &l, &r)) {segent_seive (L, R); int Mmax, mmin; int Lmax, Rmax, Lmin, rmin; Mmax =-1, mmin = 1 << 30; int t =-1; for (int i = 0; i < len; i++) {if (Prime[i]) {if (T >= 0) {if (Mmax < i-t) Mmax = I-t, Lmax = l + t, Rmax = l + i; if (Mmin > i-t) mmin = i-t, lmin = l + t, Rmin = l + i; t = i; } else T = i; }} if (Mmax! =-1 | | Mmin! = 1 <<) printf ("%d,%d is closest,%d,%d is most distant.\n", Lmin, Rmin, Lmax, Rmax); else printf ("There is no adjacent primes.\n"); } return 0;}
poj2689--Interval Sieve method--prime Distance