POJ 2689 Prime Distance (埃氏筛 区间筛)

简介: POJ 2689 Prime Distance (埃氏筛 区间筛)

原题链接(英文题面)

中文题面

acwing上wa时会有错误数据,还是很喜欢的~

**

Prime Distance

**


Description


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 and itself). The first prime numbers are 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 are the only adjacent primes that are also adjacent numbers.

Your program is given 2 numbers: L and U (1<=L< U<=2,147,483,647), and you are to find the two adjacent primes C1 and C2 (L<=C1< C2<=U) that are closest (i.e. C2-C1 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 D1 and D2 (L<=D1< D2<=U) where D1 and D2 are as distant from each other as possible (again choosing the first pair if there is a tie).

Input


Each line of input will contain two positive integers, L and U, with L < U. The difference between L and U will not exceed 1,000,000.

Output


For each L and U, the output will either be the statement that there are no adjacent primes (because there are less than two primes between the two given numbers) or a line giving the two pairs of adjacent primes.

Sample Input


2 17

14 17

Sample Output


2,3 are closest, 7,11 are most distant.

There are no adjacent primes.

Source


Waterloo local 1998.10.17


题解以后补吧

数组开小了段错误,开大了内存超限…

还有这是多组输入

#include <cstring>
#include <iostream>
#include <algorithm>
#include<stdlib.h>
#include<stdio.h>
using namespace std;
typedef long long ll;
const int N = 1e5;
int prim[N];
int judge[N];//0为素数,1为质数 
int cnt=0;
void init(){
  for(ll i=2;i<N;i++){
    if(!judge[i])
      prim[++cnt]=i;
      for(ll j=1;j<=cnt&&i*prim[j]<N;j++){
        judge[prim[j]*i]=1;
        if(i%prim[j]==0) break;
      }
  }
}
int mp[N];
int prim2[N];
int ans=0;
void qvjian(ll l,ll r){
   ans=0;
  memset(prim2,0,sizeof prim2);
  memset(mp,0,sizeof mp);
  if(l==1) l=2;
  for(int i=1;i<=cnt&&prim[i]*prim[i]<=r;i++){
    //ll x=l/prim[i];
    //x*=prim[i];
    //if(l%prim[i]==0){
  //    x+=prim[i];
  //    if(l!=2) mp[0]=1;
  //  }  
    ll x=(l+prim[i]-1)/prim[i]*prim[i];
    if(x==prim[i]) x+=prim[i];
    //cout<<prim[i]<<" "<<x<<endl;
    for(ll j=x;j<=r;j+=prim[i]) 
      mp[j-l]=1;//合数 
  }
  for(int i=0;i<=r-l;i++) if(!mp[i]) prim2[++ans]=i+l;
}
int main(){
  init();
  //for(int i=1;i<10;i++) cout<<prim[i]<<endl;
//  cout<<endl;
  ll l,r;
  while(~scanf("%lld%lld",&l,&r)){
    qvjian(l,r);
    //for(int i=1;i<=ans;i++) cout<<prim2[i]<<endl;
    if(ans<2) cout<<"There are no adjacent primes."<<endl;
    else{
      int minn=1,maxx=1;
      for(int i=1;i<ans;i++){
        int t=prim2[i+1]-prim2[i];
        if(t<prim2[minn+1]-prim2[minn]) minn=i;
        if(t>prim2[maxx+1]-prim2[maxx]) maxx=i;
      }
      printf("%d,%d are closest, %d,%d are most distant.\n",prim2[minn],prim2[minn+1],prim2[maxx],prim2[maxx+1]);
    }
  }
  return 0;
}


目录
相关文章
|
6月前
|
算法 测试技术 C#
【单调队列】LeetCode1499:满足不等式的最大值
【单调队列】LeetCode1499:满足不等式的最大值
【单调队列】LeetCode1499:满足不等式的最大值
|
12月前
|
C++
筛质数、分解质因数和快速幂的应用
筛质数、分解质因数和快速幂的应用
61 0
|
算法
poj 1050 To the Max(最大子矩阵之和)
poj 1050 To the Max(最大子矩阵之和)
36 0
|
人工智能
POJ 2299 Ultra-QuickSort(树状数组+离散化+求逆序数)
POJ 2299 Ultra-QuickSort(树状数组+离散化+求逆序数)
51nod 1711 平均数(二分 + 树状数组好题)
51nod 1711 平均数(二分 + 树状数组好题)
95 0
AcWing 246. 区间最大公约数 (gcd性质 线段树)
AcWing 246. 区间最大公约数 (gcd性质 线段树)
107 0
AcWing 246. 区间最大公约数 (gcd性质 线段树)
PTA 7-4 素数等差数列 (20 分)
2004 年,陶哲轩(Terence Tao)和本·格林(Ben Green)证明了:对于任意大的 n,均存在 n 项全由素数组成的等差数列。
110 0
|
Go
[Nowcoder / POJ2728] 最优比率生成树 | 二分 + prim
有n个点,其中,每个点给出位置坐标( x , y ) 以及高度z ,两点之间的距离为两点之间的欧几里得距离 两点之间建立一条路的代价为两点之间的高度差,问将n 个点联通的情况下,求出最大的cost/dis
127 0
|
算法
【欧拉计划第 10 题】 质数之和 Summation of primes
【欧拉计划第 10 题】 质数之和 Summation of primes
111 0