需要用到一个性质,任意合数 $x$,必然有一个 $\leqslant \sqrt{x}$ 的质因子
$\textbf{i. 用筛法筛出 } [2, \sqrt{R}]$ 之间所有的质数
$\textbf{ii. for } \forall p \in primes[…]$
$\quad \quad \textbf{for } \forall i \in \lfloor L/p \rfloor \cdots \lfloor R/p \rfloor \quad v[i \times p] = 1$ 标记合数
$\textbf{iii. 线性扫描 }[L,R],$ 得到所有质数,并且计算最短距离
unordered_map版本
ZOJ 可以 AC
ZOJ1842
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <vector>
#include <stack>
#include <map>
#include <set>
#include <sstream>
#include <iomanip>
#include <cmath>
#include <bitset>
#include <assert.h>
#include <unordered_map>
using namespace std;
typedef long long ll;
typedef set<int>::iterator ssii;
#define Cmp(a, b) memcmp(a, b, sizeof(b))
#define Cpy(a, b) memcpy(a, b, sizeof(b))
#define Set(a, v) memset(a, v, sizeof(a))
#define debug(x) cout << #x << ": " << x << endl
#define _forS(i, l, r) for(set<int>::iterator i = (l); i != (r); i++)
#define _rep(i, l, r) for(int i = (l); i <= (r); i++)
#define _for(i, l, r) for(int i = (l); i < (r); i++)
#define _forDown(i, l, r) for(int i = (l); i >= r; i--)
#define debug_(ch, i) printf(#ch"[%d]: %d\n", i, ch[i])
#define debug_m(mp, p) printf(#mp"[%d]: %d\n", p->first, p->second)
#define debugS(str) cout << "dbg: " << str << endl;
#define debugArr(arr, x, y) _for(i, 0, x) { _for(j, 0, y) printf("%c", arr[i][j]); printf("\n"); }
#define _forPlus(i, l, d, r) for(int i = (l); i + d < (r); i++)
#define lowbit(i) (i & (-i))
#define MPR(a, b) make_pair(a, b)
pair<int, int> crack(int n) {
int st = sqrt(n);
int fac = n / st;
while (n % st) {
st += 1;
fac = n / st;
}
return make_pair(st, fac);
}
inline ll qpow(ll a, int n) {
ll ans = 1;
for(; n; n >>= 1) {
if(n & 1) ans *= 1ll * a;
a *= a;
}
return ans;
}
template <class T>
inline bool chmax(T& a, T b) {
if(a < b) {
a = b;
return true;
}
return false;
}
template <class T>
inline bool chmin(T& a, T b) {
if(a > b) {
a = b;
return true;
}
return false;
}
bool _check(int x) {
//
return true;
}
int bsearch1(int l, int r) {
while (l < r) {
int mid = (l + r) >> 1;
if(_check(mid)) r = mid;
else l = mid + 1;
}
return l;
}
int bsearch2(int l, int r) {
while (l < r) {
int mid = (l + r + 1) >> 1;
if(_check(mid)) l = mid;
else r = mid - 1;
}
return l;
}
// ============================================================== //
const int maxn = 1e5 + 10;
const ll inf = 0x3f3f3f3f3f3f3f3f;
ll L, R;
void primes(ll n, vector<ll>& vec) {
bool v[maxn];
memset(v, 0, sizeof(v));
for(ll i = 2; i <= n; i++) {
if(v[i]) continue;
vec.push_back(i);
for(ll j = i; j < n/i; j++) v[i*j] = 1;
}
}
void solve() {
vector<ll> vec;
primes(sqrt(R), vec);
unordered_map<ll, int> fl;
for(auto p : vec) {
for(ll i = (L)/p; i <= (R)/p; i++) if(i > 1) fl[i*p] = 1;
}
vector<ll> res;
for(ll i = max(2ll, L); i <= R; i++) if(fl[i] == 0) res.push_back(i);
if(res.size() < 2) printf("There are no adjacent primes.\n");
else {
ll ans1 = -inf, ans2 = inf;
ll a, b, c, d;
for(int i = 0; i < res.size()-1; i++) {
if(chmax(ans1, res[i+1] - res[i])) c = res[i], d = res[i+1];
if(chmin(ans2, res[i+1] - res[i])) a = res[i], b = res[i+1];
}
printf("%lld,%lld are closest, %lld,%lld are most distant.\n", a, b, c, d);
}
}
void init() {
//
}
int main() {
//freopen("input.txt", "r", stdin);
while (scanf("%lld%lld", &L, &R) == 2) {
init();
solve();
}
}
数组版本
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <vector>
#include <stack>
#include <map>
#include <set>
#include <sstream>
#include <iomanip>
#include <cmath>
#include <bitset>
#include <assert.h>
#include <unordered_map>
using namespace std;
typedef long long ll;
typedef set<int>::iterator ssii;
#define Cmp(a, b) memcmp(a, b, sizeof(b))
#define Cpy(a, b) memcpy(a, b, sizeof(b))
#define Set(a, v) memset(a, v, sizeof(a))
#define debug(x) cout << #x << ": " << x << endl
#define _forS(i, l, r) for(set<int>::iterator i = (l); i != (r); i++)
#define _rep(i, l, r) for(int i = (l); i <= (r); i++)
#define _for(i, l, r) for(int i = (l); i < (r); i++)
#define _forDown(i, l, r) for(int i = (l); i >= r; i--)
#define debug_(ch, i) printf(#ch"[%d]: %d\n", i, ch[i])
#define debug_m(mp, p) printf(#mp"[%d]: %d\n", p->first, p->second)
#define debugS(str) cout << "dbg: " << str << endl;
#define debugArr(arr, x, y) _for(i, 0, x) { _for(j, 0, y) printf("%c", arr[i][j]); printf("\n"); }
#define _forPlus(i, l, d, r) for(int i = (l); i + d < (r); i++)
#define lowbit(i) (i & (-i))
#define MPR(a, b) make_pair(a, b)
pair<int, int> crack(int n) {
int st = sqrt(n);
int fac = n / st;
while (n % st) {
st += 1;
fac = n / st;
}
return make_pair(st, fac);
}
inline ll qpow(ll a, int n) {
ll ans = 1;
for(; n; n >>= 1) {
if(n & 1) ans *= 1ll * a;
a *= a;
}
return ans;
}
template <class T>
inline bool chmax(T& a, T b) {
if(a < b) {
a = b;
return true;
}
return false;
}
template <class T>
inline bool chmin(T& a, T b) {
if(a > b) {
a = b;
return true;
}
return false;
}
bool _check(int x) {
//
return true;
}
int bsearch1(int l, int r) {
while (l < r) {
int mid = (l + r) >> 1;
if(_check(mid)) r = mid;
else l = mid + 1;
}
return l;
}
int bsearch2(int l, int r) {
while (l < r) {
int mid = (l + r + 1) >> 1;
if(_check(mid)) l = mid;
else r = mid - 1;
}
return l;
}
// ============================================================== //
const int maxn = 1e6 + 10;
const ll inf = 0x3f3f3f3f3f3f3f3f;
ll L, R;
void primes(ll n, vector<ll>& vec) {
bool v[maxn];
memset(v, 0, sizeof(v));
for(ll i = 2; i <= n; i++) {
if(v[i]) continue;
vec.push_back(i);
for(ll j = i; j < n/i; j++) v[i*j] = 1;
}
}
void solve() {
vector<ll> vec;
primes(sqrt(R), vec);
//unordered_map<ll, int> fl;
int fl[maxn];
memset(fl, 0, sizeof(fl));
for(auto p : vec) {
for(ll i = (L)/p; i <= (R)/p; i++) if(i > 1 && i*p-L >= 0) fl[i*p-L] = 1;
}
vector<ll> res;
for(ll i = max(2ll, L); i <= R; i++) if(i >= L && fl[i-L] == 0) res.push_back(i);
if(res.size() < 2) printf("There are no adjacent primes.\n");
else {
ll ans1 = -inf, ans2 = inf;
ll a, b, c, d;
for(int i = 0; i < res.size()-1; i++) {
if(chmax(ans1, res[i+1] - res[i])) c = res[i], d = res[i+1];
if(chmin(ans2, res[i+1] - res[i])) a = res[i], b = res[i+1];
}
printf("%lld,%lld are closest, %lld,%lld are most distant.\n", a, b, c, d);
}
}
void init() {
//
}
int main() {
//freopen("input.txt", "r", stdin);
while (scanf("%lld%lld", &L, &R) == 2) {
init();
solve();
}
}