141MS AC
#include<stdio.h>
#include<string.h>
#include<math.h>
#define inf 500000
unsigned prime[50005],b[5000005],a[500005],tot;
unsigned L,U;
int b_search(int l,int r,int x)
{
int mid;
while (l<=r)
{
mid=(l+r)/2;
if (prime[mid]==x)
return mid;
else
if (prime[mid]<x)
l=mid+1;
else
r=mid-1;
}
return l;
}
int Getprime()
{
int i,j;
memset(b,0,sizeof(b));
tot=0;
i=2;
while (i<inf)
{
while (b[i]) i++;
prime[++tot]=i;
j=i;
while (j<=inf)
{
b[j]=1;
j+=i;
}
}
tot--;
}
int main()
{
unsigned int i,j,k,total;
unsigned int max,min,maxi,mini;
Getprime();
while (scanf("%d%d",&L,&U)==2)
{
memset(b,0,sizeof(b));
k=b_search(1,tot,(int)sqrt(U*1.0)+1.0);
i=1;
while (i<=k+1) //这里改为k+1就AC了!!!呜呜呜啎
{
j=L%prime[i];
if (j>0)
j=prime[i]-j;
while (j<=U-L)
{
if (j+L!=prime[i])
b[j]=1;
j+=prime[i];
}
i++;
}
total=0;
for (i=0;i<=U-L ;i++ )
if (!b[i]&&L+i>=2)
a[++total]=L+i;
if (total<2)
printf("There are no adjacent primes.\n");
else
{
max=min=a[2]-a[1];maxi=1;mini=1;
for (i=3;i<=total ;i++ )
{
if (a[i]-a[i-1]>max)
{
max=a[i]-a[i-1];
maxi=i-1;
}
if (a[i]-a[i-1]<min)
{
min=a[i]-a[i-1];
mini=i-1;
}
}
printf("%d,%d are closest, %d,%d are most distant.\n",a[mini],a[mini+1],a[maxi],a[maxi+1]);
}
}
return 0;
}