#include<bits/stdc++.h>
#define sf scanf
#define pf printf
#define LL long long
#define REP(i,MAX) for(int i = 0; i<MAX; i++)
#define MAX INT_MAX
#define maxL (1000000>>5)+1
#define GET(x) (mark[x>>5]>>(x&31)&1)
#define SET(x) (mark[x>>5]|=1<<(x&31))
using namespace std;
int mark[maxL];
int p[700000], pt = 0;
void sieve() {
register int i, j, k;
SET(1);
int n = 1000000;
for(i = 2; i <= n; i++) {
if(!GET(i)) {
p[pt++] = i;
for(k = n/i, j = i*k; k >= i; k--, j -= i)
SET(j);
}
}
}
int isprime(int n) {
if(n < 1000000)
return !GET(n);
static int sq, i;
sq = (int)sqrt(n);
for(i = 0; i < pt && p[i] <= sq; i++)
if(n%p[i] == 0)
return 0;
return 1;
}
int main()
{
ios_base::sync_with_stdio(false);
sieve();
int L , U;
while(cin >> L >> U){
int last = -1 ,x1,x2,x3,x4;
int close = INT_MAX, dis = -close;
bool used = false;
for (LL i = L; i <= U; ++i)
{
if(isprime(i)){
if(last == -1)last = i;
else{
if(i-last > dis){
dis = i-last;
x1 = last , x2 = i;
}
if(i-last < close){
close = i-last;
x3 = last, x4 = i;
}
last=i;
used=true;
}
}
}
if(!used)cout <<"There are no adjacent primes.\n";
else cout <<x3<<','<<x4<<" are closest, "<<x1<<','<<x2<<" are most distant.\n";
}
return 0;
}
#define sf scanf
#define pf printf
#define LL long long
#define REP(i,MAX) for(int i = 0; i<MAX; i++)
#define MAX INT_MAX
#define maxL (1000000>>5)+1
#define GET(x) (mark[x>>5]>>(x&31)&1)
#define SET(x) (mark[x>>5]|=1<<(x&31))
using namespace std;
int mark[maxL];
int p[700000], pt = 0;
void sieve() {
register int i, j, k;
SET(1);
int n = 1000000;
for(i = 2; i <= n; i++) {
if(!GET(i)) {
p[pt++] = i;
for(k = n/i, j = i*k; k >= i; k--, j -= i)
SET(j);
}
}
}
int isprime(int n) {
if(n < 1000000)
return !GET(n);
static int sq, i;
sq = (int)sqrt(n);
for(i = 0; i < pt && p[i] <= sq; i++)
if(n%p[i] == 0)
return 0;
return 1;
}
int main()
{
ios_base::sync_with_stdio(false);
sieve();
int L , U;
while(cin >> L >> U){
int last = -1 ,x1,x2,x3,x4;
int close = INT_MAX, dis = -close;
bool used = false;
for (LL i = L; i <= U; ++i)
{
if(isprime(i)){
if(last == -1)last = i;
else{
if(i-last > dis){
dis = i-last;
x1 = last , x2 = i;
}
if(i-last < close){
close = i-last;
x3 = last, x4 = i;
}
last=i;
used=true;
}
}
}
if(!used)cout <<"There are no adjacent primes.\n";
else cout <<x3<<','<<x4<<" are closest, "<<x1<<','<<x2<<" are most distant.\n";
}
return 0;
}
No comments:
Post a Comment