POJ2689:素数区间筛选

Prime Distance

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 15820   Accepted: 4202

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.注意:L可能为1
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
typedef long long ll;
const int MAXN=1000005;
const int INF=0x7f7f7f7f;
int l,u;
int prime[MAXN],top;
bool isPrime_small[MAXN];
bool isPrime[MAXN];
void segment_sieve()
{
    memset(isPrime_small,true,sizeof(isPrime_small));
    memset(isPrime,true,sizeof(isPrime));
    isPrime_small[1]=false;
    for(ll i=2;i*i<=u;i++) //注意防止i,j溢出,要设为long long
    {
        if(isPrime_small[i])
        {
            for(ll j=i+i;j*j<=u;j+=i)
                isPrime_small[j]=false;
            for(ll j=max(((l+i-1)/i)*i,2*i);j<=u;j+=i)
                isPrime[j-l]=false;
        }
    }
}
void sieve()
{
    for(ll i=l;i<=u;i++)
    {
        if(isPrime[i-l])
            prime[top++]=i;
    }
}
int main()
{
    while(scanf("%d%d",&l,&u)!=EOF)
    {
        if(l==1)    l++;
        top=0;
        segment_sieve();
        sieve();
        if(top<=1)
        {
            printf("There are no adjacent primes.\n");
        }
        else
        {
            int a,b;
            int mn=INF;
            int c,d;
            int mx=0;
            for(int i=1;i<top;i++)
            {
                if(prime[i]-prime[i-1]<mn)
                {
                    a=prime[i-1];
                    b=prime[i];
                    mn=b-a;
                }
                if(prime[i]-prime[i-1]>mx)
                {
                    c=prime[i-1];
                    d=prime[i];
                    mx=d-c;
                }
            }
            printf("%d,%d are closest, %d,%d are most distant.\n",a,b,c,d);
        }
    }
    return 0;
}
时间: 2024-08-08 00:03:59

POJ2689:素数区间筛选的相关文章

POJ-2689-Prime Distance(素数区间筛法)

链接: https://vjudge.net/problem/POJ-2689 题意: 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

HDOJ/HDU 2710 Max Factor(素数快速筛选~)

Problem Description To improve the organization of his farm, Farmer John labels each of his N (1 <= N <= 5,000) cows with a distinct serial number in the range 1..20,000. Unfortunately, he is unaware that the cows interpret some serial numbers as be

POJ 2689 Prime Distance(素数区间筛法--经典题)

大致题意:给定[L,R]区间,找出区间内的每个素数 数据范围 : 1<=L< R<=2,147,483,647) R-L <=1,000,000. R的数值太大,所以不能直接筛[0,R]的,要空间和时间优化,用到区间筛法,另外注意不能用int,因为R和L都是满int的,中间有很多细节处理会爆int的,还要注意1不是素数,所以在区间筛中要特判一下,是个易错的地方 //1160K 16MS C++ 1539B #include<cstdio> #include<ios

数组拷贝、数组函数、通过数组函数来模拟数据结构的栈和队列、回调的意义、数组函数的排序问题、算法以及寻找素数的筛选法

1.数组的拷贝数组拷贝时指针的指向问题. 数组在拷贝时,指针的位置随之复制[这一点拷贝是完全一样]但是如果拷贝的数组的指针是非法的,那么拷贝出新指针的位置进行初始化<?php$arr1=array('123');end($arr1);next($arr1);//这个指针非法$arr2 = $arr1;//这里进行数组的拷贝var_dump(current($arr2));//得到指向‘123’元素的指针var_dump(current($arr1));//此时这个数组的指针有问题?> 但是拷贝

HDOJ(HDU) 2138 How many prime numbers(素数-快速筛选没用上、)

Problem Description Give you a lot of positive integers, just to find out how many prime numbers there are. Input There are a lot of cases. In each case, there is an integer N representing the number of integers to find. Each integer won't exceed 32-

素数高效率筛选法

无需多言直接上代码吧! 1 //Eratosthenes 筛法(埃拉托斯特尼筛法) 2 memset(check, false, sizeof(check)) 3 int tot = 0; 4 for(int i=2; i<=N; i++) 5 if(!check[i]) 6 { 7 prime[tot++] = i; 8 for(int j=i*2; j<=N; j+=i) 9 check[j] = true; 10 } 11 12 //Euler 筛法(欧拉筛法)--线性筛选 13 mem

获取小于N的素数 优化筛选法的C++实现

孪生素数(间隔为2的相邻素数)的相关定理与推论 P1: 当 N 不小于 6 且 N-1 和 N+1 为 孪生素数, 则 N 一定是 6的倍数 T1:当 N 不小于 1 且 N=6x-1 或 N=6x+1 不是素数, 那么 N 一定不是 2和 3的倍数 P2:当N 不小于 5 时,若 N 为素数,那么N mod 6 =1或N mod 6 = 5 T2: 一个大于5的数有且只有整除6余数是 1 或者 5 才有可能是素数 一个数 整除6 的余数 可能是 1,2,3,4,5 但是 余数 为2,3,4的情

素数的筛选和bool类型的妙用

1 #include<iostream> 2 using namespace std; 3 #define max 10000 4 bool num[10002]; 5 int pan1,pan2; 6 void foundprime(){ 7 for(int i=2;i<=100;i++) 8 { 9 for(int j=i*i;j<=max;j+=i) 10 { if(num[j]==true) 11 num[j]=false; 12 } 13 } 14 } 15 void c

素数的筛选法

1 #include <cstdio> 2 #include <cstdlib> 3 #include <cstring> 4 #include <algorithm> 5 #include <iostream> 6 #include <cmath> 7 8 #define MAX 1000000 9 10 using namespace std; 11 int num[MAX]; 12 13 int main(int argc, c