poj2689 素数距离(埃氏筛法分块筛+暴力枚举)


#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll INFF=1e12;
const int INF=0x7f7f7f7f;

ll l,r;
int cnt;
int prim[50010];
bool vis[1000010];
ll prim2[1000010];
void init()
{
    cnt=0;
    memset(vis,0,sizeof(vis));
    for(int i=2;i<=50000;i++)
    {
        if(!vis[i])
        {
            prim[cnt++]=i;
            for(ll j=i+i;j<=50000;j+=i)
                vis[j]=1;
                //printf("%d\n",i);
        }
    }
}

void init2()
{
    ll i,j,b;
    memset(vis,0,sizeof(vis));
    for(i=0;i=l)
                vis[j-l]=1;
        }
    }
    if(l==1)
        vis[0]=1;
}

void solve()
{
    init2();
    int cntt=0;
    for(int i=0;i<=r-l;i++)
    {
        if(!vis[i])
        {
            prim2[cntt++]=i+l;
        }
    }
    ll minn=INFF,maxx=-INFF;
    ll maxl,maxr,minl,minr;
    if(cntt<=1)
    {
        printf("There are no adjacent primes.\n");
    }
    else
    {
        for(int i=0;imaxx)
            {
                maxx=prim2[i+1]-prim2[i];
                maxl=prim2[i],maxr=prim2[i+1];
            }
        }
        printf("%lld,%lld are closest, %lld,%lld are most distant.\n",minl,minr,maxl,maxr);
    }
}

int main()
{
    init();
    while(scanf("%lld%lld",&l,&r)!=EOF)
    {
        solve();
    }
    return 0;
}

你可能感兴趣的:(数学--数论)