标签:distance stdio.h apt NPU ble with als int algo
Description
Input
Output
Sample Input
2 17
14 17
Sample Output
2,3 are closest, 7,11 are most distant.
There are no adjacent primes.
code
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;
const int Mr=1e6,Mx=5e4+10;
int l,r,cnt,pri[Mx];
bool ispr[Mx],f[Mr];
void ycl()
{
memset(ispr,1,sizeof(ispr));
ispr[1]=0;
int m=sqrt(Mx+0.5);
for(int i=2;i<=m;++i) if(ispr[i]) {
for(int j=i+i;j<=Mx;j+=i) ispr[j]=0;
}
for(int i=2;i<=Mx;++i) {
if(ispr[i]) pri[++cnt]=i;
}
}
int main()
{
ycl();
while(scanf("%d%d",&l,&r)!=EOF)
{
if(l==1) l=2;
memset(f,0,sizeof(f));
for(int i=1;i<=cnt;++i)
{
int a=(l-1)/pri[i]+1;
int b=r/pri[i];
for(int j=a;j<=b;++j) if(j>1) f[pri[i]*j-l]=1;
}
int p=-1,mx=-1,mn=1e9,xx,yx,xn,yn;
for(int i=0;i<=r-l;++i) {
if(!f[i]) {
if(p!=-1) {
if(mx<i-p) {
mx=i-p;
xx=p+l,yx=i+l;
}
if(mn>i-p) {
mn=i-p;
xn=p+l,yn=i+l;
}
}
p=i;
}
}
if(mx==-1) printf("There are no adjacent primes.\n");
else printf("%d,%d are closest, %d,%d are most distant.\n",xn,yn,xx,yx);
}
return 0;
}
标签:distance stdio.h apt NPU ble with als int algo
原文地址:https://www.cnblogs.com/qseer/p/9768487.html