标签:memset ++ ring cstring 注意 clu span int down
简单的素数筛,要注意mn的初值要大于r-l+1……
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
typedef long long ll;
bool is[50010],wa[1000010];
ll l,r,x,y,prime[1000010],yxd;
int main()
{
memset(is,1,sizeof(is));
for (ll i=2;i<=50000;i++)
{
if (is[i])
{
prime[++yxd]=i;
for (ll j=i*i;j<=1000000;j+=i)
is[j]=0;
}
}
while(~scanf("%lld%lld",&l,&r))
{
ll pre=0,mn=r-l+2,mx=0,fir=0,lst=0,cnt=0;
x=0,y=0;
for (int i=0;i<=mn;i++) wa[i]=1;
if (1-l>=0) wa[1-l]=0;
for (ll i=1;i<=yxd && prime[i]*prime[i]<=r;i++)
{
ll j=l/prime[i]*prime[i];
for (;j<=r;j+=prime[i])
if (j>=l && j>prime[i]) wa[j-l]=0;
}
for (ll i=l;i<=r;i++)
if (wa[i-l])
{
if (pre)
{
if (i-pre+1<mn) x=pre,y=i,mn=i-pre+1;
if (i-pre+1>mx) fir=pre,lst=i,mx=i-pre+1;
}
pre=i;
cnt++;
}
if (cnt<2) printf("There are no adjacent primes.\n");
else printf("%lld,%lld are closest, %lld,%lld are most distant.\n",x,y,fir,lst);
}
return 0;
}
标签:memset ++ ring cstring 注意 clu span int down
原文地址:http://www.cnblogs.com/mrha/p/7929631.html