素数筛||素数距离

蒜头君请你求出区间 [l,r][l,r]      [l,r][l,r] 上距离最近的相邻的素数对和距离最远的相邻的素数对。3,53,5      3,53,5 是相邻的素数,2,52,5      2, 52,5 不是相邻的素数。距离定义为 22      22 个素数的差的绝对值。比如5,7 距离为 2。
输入格式
输入 22      22 个整数 l,r(1≤l≤r≤8000000)l,r(1≤l≤r≤8000000)      l, r(1 \le l \le r \le 8000000)l,r(1≤l≤r≤8000000)
输出格式
如果 a,b(ab)a,b(ab)      a, b(ab)a,b(ab) 是距离最近的素数对,c,d(cd)c,d(cd)      c,d(cd)c,d(cd) 是距离最远的素数对,按照如下格式输出a,b are closest, c,d are most distant.。
如果最近或者最远有多对,输出 aa      aa 和 c​c​      c​c​ 最小的。如果没有相邻是素数对,输出There are no adjacent primes.。

样例输入

3 10

样例输出

3,5 are closest, 3,5 are most distant.

样例输入

14 17

样例输出

There are no adjacent primes.

#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
#define MAX 8000004
typedef int ll;
ll map[MAX];
ll is_prime(ll m)
{
	map[1]=1;
	map[0]=1;
	for(ll i = 2;i*i<m;++i)
	{
		if(!map[i])
		{
			for(ll j=i*i;j<=m;j+= i)
			{
				map[j]=1;
			}
		}
	}
}
int main()
{
	ll n,m,i,count=0;
	cin>>n>>m;
	if(n>m)
	{
	 n^=m;
	 m^=n;
	 n^=m;
	}
	is_prime(m);
	vector<ll> a;
	for(i=n;i<=m;++i)
	{
		if(!map[i])
		{
			a.push_back(i);
		}
	}
	int far=0,shortly=MAX,flag=0;
	i=0;
	int far_x=0,far_y=0,short_x=0,short_y=0;
//	cout<<a.size()<<endl;
	for(;i+1<a.size();i++)
	{
			if((a[i+1]-a[i])>far)
			{
			     flag=1;
				 far_x=a[i];
				 far_y=a[i+1];
				 far = a[i+1]-a[i];
		    }
			if((a[i+1]-a[i])<shortly)
		   {
			 flag=1;
			 short_x=a[i];
			 short_y=a[i+1];
			 shortly = a[i+1]-a[i];
		   }
    }
	if(flag)
	{
		cout<<short_x<<","<<short_y<<" are closest,"<<far_x<<","<<far_y<<" are most distant"<<endl;
	}
	else
	{
		 cout<<"There are no adjacent primes"<<endl;
	}
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Ali]e

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值