Question address: http://poj.org/problem? Id = 2689
Give you an interval L-R of no more than 1000000, ask you to find the maximum and minimum value of the adjacent prime number difference within the interval, output adjacent prime number.
ACCode:
# Include <iostream> # include <cstdio> # include <cstring> # include <string> # include <cstdlib> # include <cmath> # include <vector> # include <list> # include <deque> # include <queue> # include <iterator> # include <stack> # include <map> # include <set> # include <algorithm> # include <cctype> using namespace STD; typedef long ll; const int n = 1 <16; const int M = 1000005; const int mod = 1000007; const int INF = 0x3f3f3f; const Double Pi = ACOs (-1.0); int pri [N], K; void xh_phi () {int I, j; memset (PRI, 0, sizeof (PRI )); k = 0; for (I = 2; I <= N; I ++) {If (! PRI [I]) {pri [++ K] = I; for (j = I; j <= N; j + = I) PRI [J] = 1 ;}}} int prime [m], T; bool nopri [m]; void getprime (int l, int R) {int I, J; memset (nopri, false, sizeof (nopri); If (L <2) L = 2; for (I = 1; I <= K & (LL) PRI [I] * Pri [I] <= r; I ++) {int S = L/pri [I] + (L % pri [I]> 0 ); if (S = 1) S = 2; for (j = s; (LL) J * Pri [I] <= r; j ++) if (LL) J * Pri [I]> = L) nopri [J * Pri [I]-l] = true;} prime [0] = 0; t = 0; for (I = 0; I <= R-L; I ++) if (! Nopri [I]) {Prime [++ T] = I + L ;}} int main () {int n, m, I, j; xh_phi (); While (~ Scanf ("% d", & N, & M) {getprime (n, m); int MI = inf, MA = 0; int x1, x2, Y1, y2, f = 0; If (T <2) {printf ("there are no adjacent primes. \ n "); continue;} for (I = 1; I <t; I ++) {int P = prime [I + 1]-prime [I]; if (P> Ma) {MA = P; x2 = prime [I]; y2 = prime [I + 1];} If (P <mi) {mi = P; x1 = prime [I]; Y1 = prime [I + 1] ;}} printf ("% d, % d are closest, % d, % d are most distant. \ n ", X1, Y1, X2, Y2);} return 0 ;}