POJ 2689 (素数区间筛法)

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 1 1 and itself). The first prime numbers are 2 , 3 , 5 , 7 2,3,5,7 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 2,3 2,3 are the only adjacent primes that are also adjacent numbers.
Your program is given 2 2 2 numbers: L L L and U U U ( 1 < = L < U < = 2 , 147 , 483 , 647 1<=L< U<=2,147,483,647 1<=L<U<=2,147,483,647), and you are to find the two adjacent primes C 1 C_{1} C1 and C 2 ( L < = C 1 < C 2 < = U ) C_{2} (L<=C_{1}< C_{2}<=U) C2(L<=C1<C2<=U) that are closest ( i . e . C 2 − C 1 i.e. C_{2}-C_{1} i.e.C2C1 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 D 1 D_{1} D1 and D 2 D_{2} D2 ( L < = D 1 < D 2 < = U L<=D_{1}< D_{2}<=U L<=D1<D2<=U) where D 1 D_{1} D1 and D 2 D_{2} 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 L L and U U U, with L < U L < U L<U. The difference between L L L and U U U will not exceed 1 , 000 , 000. 1,000,000. 1,000,000.

Output

For each L L L and U U 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 2,3 2,3 are closest, 7 , 11 7,11 7,11 are most distant.
There are no adjacent primes.

题意:

给出一个区间 [ l , r ] [l, r] [l,r] 求其中相邻的距离最近和最远的素数对 。
要找到 $[l, r] $中相邻最近和最远的素数对肯定是需要找出 [ l , r ] [l, r] [l,r] 内所有素数 . 但是无论是直接线性打表还是暴力都处理不了这么大的数据 .

可以先给 s q r t ( r ) sqrt(r) sqrt(r) 内的素数打个表, 再用 s q r t ( r ) sqrt(r) sqrt(r) 内的素数去筛选 [ l , r ] [l, r] [l,r] 内的合数, 然后再遍历一次 [ l , r ] , [l, r], [l,r], 记录答案即可。

AC代码:
#include <cstdio>
#include <vector>
#include <queue>
#include <cstring>
#include <cmath>
#include <map>
#include <set>
#include <string>
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <stack>
#include <queue>
using namespace std;
#define sd(n) scanf("%d", &n)
#define sdd(n, m) scanf("%d%d", &n, &m)
#define sddd(n, m, k) scanf("%d%d%d", &n, &m, &k)
#define pd(n) printf("%d\n", n)
#define pc(n) printf("%c", n)
#define pdd(n, m) printf("%d %d\n", n, m)
#define pld(n) printf("%lld\n", n)
#define pldd(n, m) printf("%lld %lld\n", n, m)
#define sld(n) scanf("%lld", &n)
#define sldd(n, m) scanf("%lld%lld", &n, &m)
#define slddd(n, m, k) scanf("%lld%lld%lld", &n, &m, &k)
#define sf(n) scanf("%lf", &n)
#define sc(n) scanf("%c", &n)
#define sff(n, m) scanf("%lf%lf", &n, &m)
#define sfff(n, m, k) scanf("%lf%lf%lf", &n, &m, &k)
#define ss(str) scanf("%s", str)
#define rep(i, a, n) for (int i = a; i <= n; i++)
#define per(i, a, n) for (int i = n; i >= a; i--)
#define mem(a, n) memset(a, n, sizeof(a))
#define debug(x) cout << #x << ": " << x << endl
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define mod(x) ((x) % MOD)
#define gcd(a, b) __gcd(a, b)
#define lowbit(x) (x & -x)
typedef pair<int, int> PII;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const int MOD = 1e9 + 7;
const double eps = 1e-9;
const ll INF = 0x3f3f3f3f3f3f3f3fll;
const int inf = 0x3f3f3f3f;
inline int read()
{
	int ret = 0, sgn = 1;
	char ch = getchar();
	while (ch < '0' || ch > '9')
	{
		if (ch == '-')
			sgn = -1;
		ch = getchar();
	}
	while (ch >= '0' && ch <= '9')
	{
		ret = ret * 10 + ch - '0';
		ch = getchar();
	}
	return ret * sgn;
}
inline void Out(int a) //Êä³öÍâ¹Ò
{
	if (a > 9)
		Out(a / 10);
	putchar(a % 10 + '0');
}

ll gcd(ll a, ll b)
{
	return b == 0 ? a : gcd(b, a % b);
}

ll lcm(ll a, ll b)
{
	return a * b / gcd(a, b);
}
///快速幂m^k%mod
ll qpow(ll a, ll b, ll mod)
{
	if (a >= mod)
		a = a % mod + mod;
	ll ans = 1;
	while (b)
	{
		if (b & 1)
		{
			ans = ans * a;
			if (ans >= mod)
				ans = ans % mod + mod;
		}
		a *= a;
		if (a >= mod)
			a = a % mod + mod;
		b >>= 1;
	}
	return ans;
}

// 快速幂求逆元
int Fermat(int a, int p) //费马求a关于b的逆元
{
	return qpow(a, p - 2, p);
}

///扩展欧几里得
ll exgcd(ll a, ll b, ll &x, ll &y)
{
	if (b == 0)
	{
		x = 1;
		y = 0;
		return a;
	}
	ll g = exgcd(b, a % b, x, y);
	ll t = x;
	x = y;
	y = t - a / b * y;
	return g;
}

const int MAXN = 1e6 + 10;
const int MAX = 1e5;
int prime[MAX], tag[MAX], vis[MAXN], tot;

void getprime()
{
	for (int i = 2; i < MAX; i++)
	{
		if (!tag[i])
		{
			prime[tot++] = i;
			for (int j = 2; j * i < MAX; j++)
			{
				tag[j * i] = 1;
			}
		}
	}
}

ll Max(ll a, ll b)
{
	return a > b ? a : b;
}

int main()
{
	getprime();
	ll l, r;
	while (~sldd(l, r))
	{
		mem(vis, 0);
		rep(i, 0, tot - 1)
		{
			ll a = (l + prime[i] - 1) / prime[i];
			ll b = r / prime[i];
			for (int j = Max(2, a); j <= b; j++)
			{							   // 筛[l, r]内的合数
				vis[prime[i] * j - l] = 1; //减个l方便标记,输出答案时加回去即可
			}
		}
		if (l == 1)
			vis[0] = 1;
		ll cnt = -1, temp = MAXN, res = 0, x1, y1, x2, y2;
		for (int i = 0; i <= r - l; i++)
		{
			if (vis[i] == 0)
			{
				if (cnt != -1)
				{
					if (temp > i - cnt)
					{
						x1 = cnt;
						y1 = i;
						temp = i - cnt;
					}
					if (res < i - cnt)
					{
						x2 = cnt;
						y2 = i;
						res = i - cnt;
					}
				}
				cnt = i;
			}
		}
		if (res == 0)
			puts("There are no adjacent primes.");
		else
			printf("%lld,%lld are closest, %lld,%lld are most distant.\n", x1 + l, y1 + l, x2 + l, y2 + l);
	}
	return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值