POJ2689 Prime Distance(数论:素数筛选)

题目链接:传送门

题目:

Prime Distance
Time Limit: 1000MS        Memory Limit: 65536K
Total Submissions: 24073        Accepted: 6306

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.

思路:

大区间素数筛选。预处理小素数,拿去筛大素数就好了。

上kuangbin大大的模板。

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>

using namespace std;
const int MAX_N = 1e5 + 5;

int prime[100005];
void getPrime() {
    memset(prime, 0, sizeof prime);
    for (int i = 2; i < MAX_N; i++) {
        if (!prime[i]) prime[++prime[0]] = i;
        for (int j = 1; j <= prime[0] && prime[j] <= MAX_N/i; j++) {
            prime[prime[j]*i] = 1;
            if (i%prime[j] == 0) break;
        }
    }
}

bool notprime[1000005];
int prime2[MAX_N];
void getPrime2(int L, int R) {
    memset(notprime, false, sizeof notprime);
    if (L < 2) L = 2;
    for (int i = 1; i <= prime[0] && (long long)prime[i]*prime[i] <= R; i++)
        for (int j = max(2, L/prime[i] + (L%prime[i] > 0)); (long long)j*prime[i] <= R; j++)
            if ((long long)j*prime[i] >= L)
                notprime[j*prime[i]-L] = true;
    prime2[0] = 0;
    for (int i = 0; i <= R-L; i++)
        if (!notprime[i])
            prime2[++prime2[0]] = i+L;
}

int main()
{
    getPrime();
    int L, U;
    while (~scanf("%d%d", &L, &U)) {
        getPrime2(L, U);
        if (prime2[0] < 2) puts("There are no adjacent primes.");
        else {
            int x1 = 0, x2 = 1e7, y1 = 0, y2 = 0;
            for (int i = 2; i <= prime2[0]; i++) {
                if (prime2[i] - prime2[i-1] < x2 - x1)
                    x2 = prime2[i], x1 = prime2[i-1];
                if (prime2[i] - prime2[i-1] > y2 - y1)
                    y2 = prime2[i], y1 = prime2[i-1];
            }
            printf("%d,%d are closest, %d,%d are most distant.\n", x1, x2, y1, y2);
        }
    }
    return 0;
}

原文地址:https://www.cnblogs.com/Lubixiaosi-Zhaocao/p/9886421.html

时间: 2024-07-28 20:48:50

POJ2689 Prime Distance(数论:素数筛选)的相关文章

POJ 2689 Prime Distance(素数筛选)

题目链接:http://poj.org/problem?id=2689 题意:给出一个区间[L, R],找出区间内相连的,距离最近和距离最远的两个素数对.其中(1<=L<R<=2,147,483,647) R - L <= 1000000 思路:数据量太大不能直接筛选,要采用两次素数筛选来解决.我们先筛选出2 - 50000内的所有素数,对于上述范围内的数,如果为合数,则必定有2 - 50000内的质因子.换一句话说,也就是如果一个数没有2 - 50000内的质因子,那么这个数为素

UVA 10140 - Prime Distance(数论)

10140 - Prime Distance 题目链接 题意:求[l,r]区间内最近和最远的素数对. 思路:素数打表,打到sqrt(Max)即可,然后利用大的表去筛素数,由于[l, r]最多100W,所以可以去遍历一遍,找出答案.注意1的情况,一开始没判断1,结果WA了 代码: #include <stdio.h> #include <string.h> #include <algorithm> using namespace std; #define INF 0x3f

Light OJ 1356 Prime Independence 最大独立集+素数筛选

题目来源:Light OJ 1356 Prime Independence 题意:给你n个数 选出最多的数构成一个集合使得任何2个数不是另外一个数的质数倍 x!=k*y 思路:矛盾的2个数连边 并且所有数分成质因子数为奇数和偶数两部分 以质因子奇偶不同构建二分图 同奇或者同偶的数一定不是另外一个数的质数倍 判断矛盾 首先对每个数因子分解 例如x 有a1个p1质因子 a2个p2质因子...an个pn质因子 x的质因子个数为a1+a2+...+an 记为sum 判断是否存在x/p1  x/p2 ..

POJ-2689 Prime Distance (两重筛素数,区间平移)

Prime Distance Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13961   Accepted: 3725 Description The branch of mathematics called number theory is about properties of numbers. One of the areas that has captured the interest of number th

POJ-2689 Prime Distance(线性筛法)

Prime Distance Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 17021   Accepted: 4536 Description The branch of mathematics called number theory is about properties of numbers. One of the areas that has captured the interest of number th

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 prop

UVA - 1404 Prime k-tuple (素数筛选)

Description {p1,..., pk : p1 < p2 <...< pk} is called a prime k -tuple of distance s if p1, p2,..., pk are consecutive prime numbers and pk - p1 = s . For example, with k = 4 , s = 8 , {11, 13, 17, 19} is a prime 4-tuple of distance 8. Given an i

数论——素数筛选法与整数的素因子分解

筛选法 求出n以内的素数,最快的应该是筛选法. 筛选法的思路是: 要求10000以内的素数,把1-10000都列出来,1不是素数,划掉:2是素数,所有2的倍数都不是素数,划掉:取出下一个幸存的数,划掉它的所有倍数:直到所有素数找完为止. 这种做法的空间复杂度是O(n),时间复杂度O(n/logn). 1 const int Max = 1000005; 2 bool prime[Max]={0};//0表示素数,1为非素数 3 4 //筛选n以内的素数 5 void getPrime(int n

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