POJ2689 Prime Distance题解

题目

题目描述

原题

英文题目

The branch of mathematics called number theory is about properties of numbers. One of the areas that has captured the interest of number theoreticians for thousands of years is the question of primality. A prime number is a number that is has no proper factors (it is only evenly divisible by 1 1 1 and itself). The first prime numbers are 2 , 3 , 5 , 7 2,3,5,7 2,3,5,7 but they quickly become less frequent. One of the interesting questions is how dense they are in various ranges. Adjacent primes are two numbers that are both primes, but there are no other prime numbers between the adjacent primes. For example, 2 , 3 2,3 2,3 are the only adjacent primes that are also adjacent numbers.
Your program is given 2 2 2 numbers: L L L and U U U ( 1 ⩽ L &lt; U ⩽ 2 , 147 , 483 , 647 1 \leqslant L&lt; U \leqslant 2,147,483,647 1L<U2,147,483,647), and you are to find the two adjacent primes C 1 C1 C1 and C 2 C2 C2 ( L ⩽ C 1 &lt; C 2 ⩽ U L \leqslant C1&lt; C2 \leqslant U LC1<C2U) that are closest (i.e. C 2 − C 1 C2-C1 C2C1 is the minimum). If there are other pairs that are the same distance apart, use the first pair. You are also to find the two adjacent primes D 1 D1 D1 and D 2 D2 D2 ( L ⩽ D 1 &lt; D 2 ⩽ U L \leqslant D1&lt; D2 \leqslant U LD1<D2U) where D 1 D1 D1 and D 2 D2 D2 are as distant from each other as possible (again choosing the first pair if there is a tie).

中文题意

给定两个整数 L , R L,R L,R( 1 ⩽ L &lt; R ⩽ 2 , 147 , 483 , 647 1 \leqslant L&lt; R \leqslant 2,147,483,647 1L<R2,147,483,647),求闭区间 [ L , R ] \left[L,R\right] [L,R]中相邻两个质数的差的最小值和最大值是多少,分别输出这两对质数。

输入输出格式

输入格式

每行两个整数 L , R L,R L,R( 1 ⩽ L &lt; R ⩽ 2 , 147 , 483 , 647 , R − L ⩽ 1 0 6 1 \leqslant L&lt; R \leqslant 2,147,483,647,R-L \leqslant 10^6 1L<R2,147,483,647,RL106)。

输出格式

对于每个 L , R L,R L,R,输出最小值和最大值,格式参照样例。若区间内无质数,输出"There are no adjacent primes."。

输入输出样例

输入样例

2 17
14 17

输出样例

2,3 are closest, 7,11 are most distant.
There are no adjacent primes.

题解

由于 L , R L,R L,R的范围很大,所以埃氏筛法和欧拉筛法都无法生成 [ 1 , R ] \left[1,R\right] [1,R]的所有质数。但是 R − L R-L RL的范围很小且任何一个合数 n n n一定包含一个不超过 n \sqrt{n} n 的质因子,所以我们只需要用筛法求出 2 , 3 , ⋯ &ThinSpace; , n 2,3,\cdots,\sqrt{n} 2,3,,n 的所有质数。而对于每一个质数 p p p,标记 i × p ( ⌈ L p ⌉ ⩽ i ⩽ ⌈ R p ⌉ ) i \times p \left(\left\lceil\frac{L}{p}\right\rceil \leqslant i \leqslant \left\lceil\frac{R}{p}\right\rceil\right) i×p(pLipR)为合数。标记完后,剩下的所有数就是 [ L , R ] \left[L,R\right] [L,R]中的质数了。再两两比较,找出差最大和最小的就可以了。
代码如下:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int n,m,i,j,t1,t2,x1,x2,y1,y2,l,r,a[100001], b[1000001];
bool v[1000001];
void prime()//筛出2~46340之间的质数
{
	memset(v,1,sizeof(v));
	for(i=2;i<=46340;i++) if(v[i]){
		a[++n]=i;
		for(j=2;j<=46340/i;j++) v[i*j]=false;
	}
}
int main()
{
	prime();
	while(cin>>l>>r){
		memset(v,1,sizeof(v));
		if(l==1) v[0]=false;
		for(i=1;i<=n;i++) for(j=l/a[i];j<=r/a[i];j++) if(j>1) v[a[i]*j-l]=false;//去除L~R中的合数
		m=0;
		for(i=l;i<=r;i++){
			if(v[i-l]) b[++m]=i;//存储L~R中的质数
			if(i==r) break;
		}
		t1=2147483647;
		t2=0;
		for(i=1;i<m;i++){
			j=b[i+1]-b[i];
			if(j<t1){
				t1=j;
				x1=b[i];
				y1=b[i+1];
			}
			if(j>t2){
				t2=j;
				x2=b[i];
				y2=b[i+1];
			}
		}//比较差的大小
		if(!t2) cout<<"There are no adjacent primes."<<endl;
		else cout<<x1<<","<<y1<<" are closest, "<<x2<<","<<y2<<" are most distant."<<endl;
	}
	return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值