poj2689素数

Language:
Prime Distance
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 14463 Accepted: 3840

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


题意:给L和U求L和U之间相邻的距离最小和最大的两个素数。

分析:由于L和U的范围都是1<=L< U<=2,147,483,647,U-L<1e6直接枚举肯定会超时打表也打不下,而int范围内所有正整数的素因子小于50000,所以可以打一个50000的素数表,然后在vis(0~U-L)数组里边判断L到U是否为素数,可以通过前边打的素数表来判断,两次筛法解决问题。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <cctype>
#include <vector>
#include <set>
#include <map>
#include <queue>
using namespace std;
const int maxn = 50000+5;
int getprime[maxn], len;
bool prime[maxn];
bool vis[1000050];
void get_prime()
{
    memset(prime, true, sizeof(prime));
    prime[1] = false;
    prime[0] = false;
    len = 0;
    for (int i = 2; i < 50000; i++)
    {
        if (prime[i]) getprime[len++] = i;
        for (int j = 0; j < len; j++)

        {
            if (i*getprime[j]>=50000) break;
            prime[i*getprime[j]] = false;
            if (i % getprime[j] == 0) break;
        }
    }
}
int main()
{
	//freopen("in", "r", stdin);
	int l, r;
	get_prime();
	while (cin >> l >> r)
    {
        memset(vis, 0, sizeof(vis));
        for  (int i = 0; i < len; i++)
        {
            long long k  = getprime[i];
            if (k > r) break;
            long long j = l/k * k;//j一定要用long long 否则溢出
            while (j < l) j += k;
            if (j == k) j+=k;
            for (; j <= r; j+=k)
            {
                vis[j-l] = 1;
            }
        }
        if (l == 1) vis[0] = 1;
        int mmin_l = -1, mmin_r = -1, mmin = 0x7f7f7f7f;
        int mmax_l = -1, mmax_r = -1, mmax = 0;
        int tmp_l = -1;
        for (int i = 0; i <= r-l; i++)
        {
            if (!vis[i]){
                if (tmp_l == -1) {
                    tmp_l = i;
                }
                else {
                    if (mmin > i - tmp_l)
                    {
                        mmin = i - tmp_l;
                        mmin_l = tmp_l;
                        mmin_r = i;
                    }
                    if (mmax < i - tmp_l)
                    {
                        mmax = i - tmp_l;
                        mmax_l = tmp_l;
                        mmax_r = i;
                    }
                    tmp_l = i;
                }
            }
        }
        if (mmax == 0)
            puts("There are no adjacent primes.");
        else printf("%d,%d are closest, %d,%d are most distant.\n",
                    mmin_l+l,mmin_r+l,mmax_l+l,mmax_r+l);
    }
	return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值