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;
}


目录
相关文章
|
9月前
|
算法
poj 1050 To the Max(最大子矩阵之和)
poj 1050 To the Max(最大子矩阵之和)
26 0
51nod 1711 平均数(二分 + 树状数组好题)
51nod 1711 平均数(二分 + 树状数组好题)
82 0
|
机器学习/深度学习 人工智能
51nod 1055 最长等差数列 (dp好题)
51nod 1055 最长等差数列 (dp好题)
34 0
|
机器学习/深度学习 Java
【每日一题Day64】LC1799N 次操作后的最大分数和 | 状态压缩dp 状态压缩+dfs+记忆化搜索
每个数字有两种状态,删除或者未删除,因此可以使用状态压缩记录未删除的数字情况state,便于使用位运算进行状态的遍历及转移;使用状态压缩dp找到将所以数字删除的最大分数
87 0
【每日一题Day64】LC1799N 次操作后的最大分数和 | 状态压缩dp 状态压缩+dfs+记忆化搜索
AcWing 246. 区间最大公约数 (gcd性质 线段树)
AcWing 246. 区间最大公约数 (gcd性质 线段树)
91 0
AcWing 246. 区间最大公约数 (gcd性质 线段树)
|
算法
【欧拉计划第 10 题】 质数之和 Summation of primes
【欧拉计划第 10 题】 质数之和 Summation of primes
|
人工智能 数据建模 BI
POJ 3662 Telephone Lines【Dijkstra最短路+二分求解】
Telephone Lines Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7214   Accepted: 2638 Description Farmer John wants to set up a telephone line at his farm.
1123 0