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 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.

题意 : 求一下所给区间的相邻两素数的最大差值和最小差值
bool prime[47000];
bool pt[eps];

int main() {
    ll a, b;

    memset(prime, true, sizeof(prime));
    for(int i = 2; i < 47000; i++){
        if (prime[i]){
            for(int j = i+i; j < 47000; j += i){
                prime[j] = false;
            }
        }
    }

    while(~scanf("%lld%lld", &a, &b)){
        memset(pt, true, sizeof(pt));

        for(ll i = 2; i*i <= b; i++){
            if (prime[i]){
                for(ll j = i*max(2LL, (a+i-1)/i); j <= b; j += i){
                    pt[j-a] = false;
                }
            }
        }
        ll aa = -1, bb = -1, cc = -1, dd = -1;
        ll last, pr;
        ll t1 = 9999999, t2 = -1;
        int sign = 1;
        for(ll i = 0; i <= b-a; i++){
            if (pt[i]){
                pr = i + a;
                if (pr == 1) continue;
                ll cha = pr - last;

                if (sign){
                    last = pr;
                    sign = 0;
                    continue;
                }
                if (cha < t1){
                    t1 = cha;
                    aa = last;
                    bb = pr;
                }
                if (cha > t2){
                    t2 = cha;
                    cc = last;
                    dd = pr;
                }
                last = pr;
            }

        }
        if (aa == -1){
            printf("There are no adjacent primes.\n");
        }
        else {
            printf("%lld,%lld are closest, %lld,%lld are most distant.\n", aa, bb, cc, dd);
        }
    }

    return 0;
}
时间: 2024-11-09 19:21:46

poj 2689 区间素数筛的相关文章

light_oj 1197 区间素数筛

light_oj 1197 区间素数筛 M - Help Hanzo Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Status Practice LightOJ 1197 Description Amakusa, the evil spiritual leader has captured the beautiful princess Nakururu. The reason

区间素数筛模版

区间素数筛模版 筛出区间[a,b]的素数.(b-a<=10000,1<=a<=b<=2^31) 存在P中,素数个数即为P的size(). ll a,b; bool isprime[maxn]; vector<ll> prime; bool isP[maxn]; vector<ll> P; void play_prime() { memset(isprime,1,sizeof(isprime)); isprime[1]=0; for(int i=2;i<

poj 2689 Prime Distance(筛一个区间内的素数(或合数))

: [题意说明] 给你指定的范围[L, U],在这个范围内找出相邻最近和最远的两组质数,若最近或最远值相同,输出较小的那组.其中:1≤L,另U-L≤1000000. [问题分析] 此题与质数有关,显然若是能求出[L, U]之间的质数,然后从前往后扫描一遍即可出需要的结果,但问题是L与U的范围太大,是不可能在规定的时间内实现的. 但这里给我们提供了另一个条件:U-L≤1000000,如果我们只求1000000以内的素数,完全可以在规定的时间实现的!但由于所求的不是1-1000000以内的素数,所以

poj 2689 区间筛素数

由于区间的右端点非常大(INT_MAX),而区间长度相对小(100W),所以考虑区间筛法,左端点为1的情况需要特判一下. 1 #include <cstring> 2 #include <cstdio> 3 #include <cmath> 4 using namespace std; 5 6 typedef long long ll; 7 const int MAX = 9999999; 8 const int MIN = -1; 9 const int N = 50

poj 2689 巧妙地运用素数筛选

称号: 给出一个区间[L,R]求在该区间内的素数最短,最长距离. (R < 2 * 10^9 , R - L <= 10 ^ 6) 由数论知识可得一个数的因子可在开根号内得到. 所以,我们能够打出5*10^4内得素数.然后,在用一次筛法把在[L.R]内得合数找到,则剩下的就是素数了.这里要用到离散化.把一个数 x - L 保存在数组里.由于,直接保存肯定不行.可是我们发现区间特点较小.所以.能够想到离散化. #include <iostream> #include <algo

区间素数筛

题目描述 A positive integer is called a "prime-factor prime" when the number of its prime factors is prime. For example, 12 is a prime-factor prime because the number of prime factors of 12=2×2×3 is 3, which is prime. On the other hand, 210 is not a

LightOJ 1197(区间素数筛)

Help Hanzo Amakusa, the evil spiritual leader has captured the beautiful princess Nakururu. The reason behind this is he had a little problem with Hanzo Hattori, the best ninja and the love of Nakururu. After hearing the news Hanzo got extremely angr

lightoj1197区间素数筛

模板题,不过好像有点问题,当a==1的时候,答案把一也算进去了,要减去 #include<map> #include<set> #include<cmath> #include<queue> #include<stack> #include<vector> #include<cstdio> #include<cassert> #include<iomanip> #include<cstdlib

Light oj 1197 - Help Hanzo (素数筛技巧)

题目链接:http://lightoj.com/volume_showproblem.php?problem=1197 给你a和b求a到b之间的素数个数. 先在小区间素数筛,大区间就用类似素数筛的想法,把a到b之间不是素数的标记出来.因为b-a最多1e5的大小,所以每组数据的时间复杂度最多就o(1e5 log1e5). 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 using names