ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

POJ 2689 (素数区间筛法)

2020-02-08 12:00:14  阅读:221  来源: 互联网

标签:筛法 int ll POJ 2689 mod include scanf define


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 111 and itself). The first prime numbers are 2,3,5,72,3,5,72,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,32,32,3 are the only adjacent primes that are also adjacent numbers.
Your program is given 222 numbers: LLL and UUU (1<=L<U<=2,147,483,6471<=L< U<=2,147,483,6471<=L<U<=2,147,483,647), and you are to find the two adjacent primes C1C_{1}C1​ and C2(L<=C1<C2<=U)C_{2} (L<=C_{1}< C_{2}<=U)C2​(L<=C1​<C2​<=U) that are closest (i.e.C2C1i.e. C_{2}-C_{1}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 D1D_{1}D1​ and D2D_{2}D2​ (L<=D1<D2<=UL<=D_{1}< D_{2}<=UL<=D1​<D2​<=U) where D1D_{1}D1​ and D2D_{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, LLL and UUU, with L<UL < UL<U. The difference between LLL and UUU will not exceed 1,000,000.1,000,000.1,000,000.

Output

For each LLL and UUU, 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,32,32,3 are closest, 7,117,117,11 are most distant.
There are no adjacent primes.

题意:

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

可以先给 sqrt(r)sqrt(r)sqrt(r) 内的素数打个表, 再用 sqrt(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;
}
邵光亮 发布了631 篇原创文章 · 获赞 399 · 访问量 20万+ 他的留言板 关注

标签:筛法,int,ll,POJ,2689,mod,include,scanf,define
来源: https://blog.csdn.net/qq_43627087/article/details/104220811

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有