链接:
https://www.acwing.com/problem/content/198/
题意:
给定两个整数L和U,你需要在闭区间[L,U]内找到距离最接近的两个相邻质数C1和C2(即C2-C1是最小的),如果存在相同距离的其他相邻质数对,则输出第一对。
同时,你还需要找到距离最远的两个相邻质数D1和D2(即D1-D2是最大的),如果存在相同距离的其他相邻质数对,则输出第一对。
思路:
筛除,l-r中的素数, 使用区间筛法, 先筛出1-sqrt(r), 再筛l-r.
记录l-r的素数,挨个判断.
代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int MAXN = 1e6+10;
int IsPri1[MAXN], IsPri2[MAXN];
LL Pri[MAXN], Res[MAXN];
LL l, r;
int pos;
void Euler()
{
memset(IsPri1, 0, sizeof(IsPri1));
memset(IsPri2, 0, sizeof(IsPri2));
pos = 0;
for (int i = 2;i <= sqrt(r);i++)
{
if (IsPri1[i] == 0)
Pri[++pos] = i;
for (int j = 1;j <= pos && Pri[j]*i <= sqrt(r);j++)
{
IsPri1[Pri[j]*i] = 1;
if (i%Pri[j] == 0)
break;
}
}
for (int i = 1;i <= pos;i++)
{
for (LL j = max(2LL, (0LL+l+Pri[i]-1)/Pri[i])*Pri[i];j <= r;j += Pri[i])
IsPri2[j-l+1] = 1;
}
}
int main()
{
while (~scanf("%lld%lld", &l, &r))
{
Euler();
int cnt = 0;
int sl, sr, bl, br;
int sv = 1e9, bv = 0;
if (l == 1)
IsPri2[l] = 1;
for (int i = 1;i <= r-l+1;i++)
{
if (IsPri2[i] == 0)
Res[++cnt] = i+l-1;
}
if (cnt < 2)
printf("There are no adjacent primes.
");
else
{
for (int i = 1;i < cnt;i++)
{
if (Res[i+1]-Res[i] < sv)
{
sv = Res[i+1]-Res[i];
sl = Res[i], sr = Res[i+1];
}
if (Res[i+1]-Res[i] > bv)
{
bv = Res[i+1]-Res[i];
bl = Res[i], br = Res[i+1];
}
}
printf("%d,%d are closest, %d,%d are most distant.
", sl, sr, bl, br);
}
}
return 0;
}