POJ2689 ZOJ1842 UVA10140 Prime Distance【筛选法】

Prime Distance
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 25099 Accepted: 6567

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

问题链接POJ2689 ZOJ1842 UVA10140 Prime Distance
问题简述:(略)
问题分析
????求[l,r]区间内最近和最远的素数对。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C++语言程序如下:

/* POJ2689 ZOJ1842 UVA10140 Prime Distance */

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>

using namespace std;

const long long SUP = 0x7FFFFFFFFFFFFFFF;
const int N = 100005;
bool vis[N * 10];
int primek[N / 10], pcnt;

int main()
{
    memset(vis, false, sizeof(vis));
    for (long long i = 2; i < N; i++) {
        if (vis[i]) continue;
        primek[pcnt++] = i;
        for (long long j = i * i; j < N; j += i)
            vis[j] = true;
    }

    long long l, r;
    while (~scanf("%lld%lld", &l, &r)) {
        memset(vis, 0, sizeof(vis));
        for (long long i = 0; i < pcnt; i++)
            for (long long j = ((l + primek[i] - 1) / primek[i]) * primek[i]; j <= r; j += primek[i])
                if (j / primek[i] != 1)
                    vis[j - l] = 1;

        long long pre = -1;
        long long maxv = 0, minv = SUP;
        long long maxl, maxr, minl, minr, flag = 1;
        for (long long i = l; i <= r; i++) {
            if (vis[i - l] || i == 1) continue;
            if (pre != -1) {
                if (i - pre > maxv) {
                    maxv = i - pre;
                    maxl = pre;
                    maxr = i;
                }
                if (i - pre < minv) {
                    minv = i - pre;
                    minl = pre;
                    minr = i;
                }
                flag = 0;
            }
            pre = i;
        }
        if (flag)
            printf("There are no adjacent primes.\n");
        else
            printf("%lld,%lld are closest, %lld,%lld are most distant.\n", minl, minr, maxl, maxr);
    }
    return 0;
}

原文地址:https://www.cnblogs.com/tigerisland45/p/10367539.html

时间: 2024-09-30 18:53:34

POJ2689 ZOJ1842 UVA10140 Prime Distance【筛选法】的相关文章

hdu how many prime numbers 筛选法求素数

/* * hdu How many prime numbers * date 2014/5/13 * state AC */ #include <iostream> #include <cmath> #include <cstdio> using namespace std; bool isPrime(int x) { int sqr=sqrt(x*1.0); for(int i=2;i<=sqr;i++) { if(x%i==0)return false; }

[ACM] POJ 2689 Prime Distance (筛选范围大素数)

Prime Distance Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12811   Accepted: 3420 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

http://poj.org/problem?id=2689 /* 区间筛法适用于L, R范围比较大,但是区间长度较小时 套个模板然后取出最小最大就可以 */ /************************************************ * Author :Powatr * Created Time :2015-8-17 9:38:50 * File Name :POJ2689.cpp ********************************************

UVA10140 Prime Distance

题意翻译 大致意思:给定两个整数L,R(1<=L<=R<=2147483648,R-L<=1000000),求闭区间 [L,R]中相邻两个质数的差的最小值和最大值是多少,分别输出这两个质数. [输入格式] 输入有若干行,每行两个整数 L,R [输出格式] 对于每个L,R输出最小值和最大值,格式参照样例.若区间内无质数,输出"There are no adjacent primes.". 分割线 看到这题,瞄一眼数据范围,噫,2的31次方,比int范围大了1,然后

POJ 2689 Prime Distance 素数筛选法应用

题目来源:POJ 2689 Prime Distance 题意:给出一个区间L R 区间内的距离最远和最近的2个素数 并且是相邻的 R-L <= 1000000 但是L和R会很大 思路:一般素数筛选法是拿一个素数 然后它的2倍3倍4倍...都不是 然后这题可以直接从2的L/2倍开始它的L/2+1倍L/2+2倍...都不是素数 首先筛选出一些素数 然后在以这些素数为基础 在L-R上在筛一次因为 R-L <= 1000000 可以左移开一个1百万的数组 #include <cstdio>

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

UVA10789 Prime Frequency【筛选法】

Given a string containing only alpha-numerals (0-9, A-Z and a-z) you have to count the frequency (the number of times the character is present) of all the characters and report only those characters whose frequency is a prime number. A prime number i

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

POJ题目2689 Prime Distance(任何区间素数筛选)

Prime Distance Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13459   Accepted: 3578 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