欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

1619: Prime Distance

程序员文章站 2022-06-04 16:10:25
...

1619: Prime Distance

//#pragma GCC optimize(2)
//#include <bits/stdc++.h>
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#define rush() int T;cin>>T;while(T--)
#define go(a) while(cin>>a)
#define ms(a,b) memset(a,b,sizeof a)
#define E 1e-8
#define debug(a) cout<<"*"<<a<<"*"<<endl
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define PAUSE system("pause")
using namespace std;
typedef long long ll;
typedef unsigned ui;
typedef unsigned long long ull;
typedef pair<int,int> Pair;
const int inf=0x7f7f7f7f;
const int mod=100003;
const int N=1e6+5;

    int n,m,t;
    ll i,j,k;
    bool vis[N];
    ll prime[N],num,pos[N];

void init(){
    for(ll i=2;i<50000;i++)
	  if(!vis[i]){
	  	prime[++num]=i;
	  	for(ll j=i*2;j<50000;j+=i)
          vis[j]=1;
	  }
}
int main()
{
    //IOS;
    init();
    ll R,L;
    while(scanf("%lld%lld",&L,&R)==2){
        ms(vis,0);
        if(L==1) vis[0]=1;//特殊标记,1 不是质数
        //将 [L,R] 区间内的合数标记出来
        for(i=1;i<=num;i++){
            for(j=L/prime[i];j<=R/prime[i];j++){
                if(j>1) vis[prime[i]*j-L]=1;
            }
        }
        int cnt=0;
        for(i=L;i<=R;i++){
            if(!vis[i-L]) pos[++cnt]=i;
        }
        ll maxx=0,minn=2147483647,x,nx,y,ny,dis;
        for(i=1;i<cnt;i++){
            dis=pos[i+1]-pos[i];
            if(dis>maxx){
                maxx=dis;
                x=pos[i+1];
                nx=pos[i];
            }
            if(dis<minn){
                minn=dis;
                y=pos[i+1];
                ny=pos[i];
            }
        }
        if(!maxx) printf("There are no adjacent primes.\n");
        else printf("%lld,%lld are closest, %lld,%lld are most distant.\n",ny,y,nx,x);
    }
    return 0;
}