POJ-2689-Prime Distance(素数区间筛法)
链接:
https://vjudge.net/problem/POJ-2689
题意:
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).
思路:
考虑素数区间筛法, 然后遍历一遍素数即可。
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<math.h>
#include<vector>
using namespace std;
typedef long long LL;
const int INF = 1e9;
const int MAXN = 1e6+10;
LL l, r;
int Isprime[MAXN];
int Prime[MAXN];
int Islarge[MAXN];
int cnt;
void Euler()
{
memset(Isprime, 0, sizeof(Isprime));
memset(Islarge, 0, sizeof(Islarge));
cnt = 0;
int n = sqrt(r);
for (int i = 2;i <= n;i++)
{
if (Isprime[i] == 0)
Prime[++cnt] = i;
for (int j = i;j <= n/i;j++)
Isprime[j*i] = 1;
}
for (int i = 1;i <= cnt;i++)
{
int s = l/Prime[i];
int e = r/Prime[i];
for (int j = max(s, 2);j <= e;j++)
{
Islarge[1LL*Prime[i]*j-l] = 1;
}
}
}
int main()
{
while(~scanf("%lld%lld", &l, &r))
{
Euler();
/*
for (int i = 1;i <= cnt;i++)
cout << Prime[i] << ' ';
cout << endl;
*/
vector<int> p;
if (l == 1)
Islarge[0] = 1;
for (int i = 0;i <= r-l;i++)
{
if (Islarge[i] == 0)
p.push_back(i);
}
if (p.size() < 2)
puts("There are no adjacent primes.");
else
{
int mmax = 0, mmin = INF;
int mal, mar, mil, mir;
for (int i = 1;i < (int)p.size();i++)
{
if (p[i]-p[i-1] > mmax)
{
mmax = p[i]-p[i-1];
mal = p[i-1];
mar = p[i];
}
if (p[i]-p[i-1] < mmin)
{
mmin = p[i]-p[i-1];
mil = p[i-1];
mir = p[i];
}
}
mil += l, mir += l, mal += l, mar += l;
printf("%d,%d are closest, %d,%d are most distant.\n", mil, mir, mal, mar);
}
}
return 0;
}
POJ-2689-Prime Distance(素数区间筛法)的更多相关文章
- poj 2689 Prime Distance(大区间素数)
题目链接:poj 2689 Prime Distance 题意: 给你一个很大的区间(区间差不超过100w),让你找出这个区间的相邻最大和最小的两对素数 题解: 正向去找这个区间的素数会超时,我们考虑 ...
- 题解报告:poj 2689 Prime Distance(区间素数筛)
Description The branch of mathematics called number theory is about properties of numbers. One of th ...
- poj 2689 Prime Distance(大区间筛素数)
http://poj.org/problem?id=2689 题意:给出一个大区间[L,U],分别求出该区间内连续的相差最小和相差最大的素数对. 由于L<U<=2147483647,直接筛 ...
- poj 2689 Prime Distance (素数二次筛法)
2689 -- Prime Distance 没怎么研究过数论,还是今天才知道有素数二次筛法这样的东西. 题意是,要求求出给定区间内相邻两个素数的最大和最小差. 二次筛法的意思其实就是先将1~sqrt ...
- poj 2689 Prime Distance(区间筛选素数)
Prime Distance Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9944 Accepted: 2677 De ...
- 数论 - 素数的运用 --- poj 2689 : Prime Distance
Prime Distance Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12512 Accepted: 3340 D ...
- [ACM] POJ 2689 Prime Distance (筛选范围大素数)
Prime Distance Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12811 Accepted: 3420 D ...
- POJ - 2689 Prime Distance (区间筛)
题意:求[L,R]中差值最小和最大的相邻素数(区间长度不超过1e6). 由于非素数$n$必然能被一个不超过$\sqrt n$的素数筛掉,因此首先筛出$[1,\sqrt R]$中的全部素数,然后用这些素 ...
- POJ 2689 Prime Distance (素数筛选法,大区间筛选)
题意:给出一个区间[L,U],找出区间里相邻的距离最近的两个素数和距离最远的两个素数. 用素数筛选法.所有小于U的数,如果是合数,必定是某个因子(2到sqrt(U)间的素数)的倍数.由于sqrt(U) ...
- POJ 2689 Prime Distance (素数+两次筛选)
题目地址:http://poj.org/problem?id=2689 题意:给你一个不超过1000000的区间L-R,要你求出区间内相邻素数差的最大最小值,输出相邻素数. AC代码: #includ ...
随机推荐
- qt 旧项目编译运行提示 “启动程序失败,路径或者权限错误?” 原因及解决方法
qt 旧项目编译运行提示 "启动程序失败,路径或者权限错误?" 原因及解决方法 原因 Qt Creator在打开项目文件的同时会生成.pro.user文件,.pro.user文件叫 ...
- Linux安装Python3流程
安装必要的依赖库文件 yum -y install zlib zlib-devel bzip2 bzip2-devel ncurses ncurses-devel readline readline- ...
- 17. Scala泛型、上下界、视图界定、上下文界定
17.1 泛型的基本介绍 17.1.1 基本介绍 1) 如果我们要求函数的参数可以接受任意类型,可以使用泛型,这个类型可以代表任意的数据类型 2) 例如List,在创建List时,可以传入整型.字符串 ...
- PB Event ID 含义 内容浅析
Event ID 含义 内容浅析 event可以用pb自带的id,自动触发事件,而function就需要你去调用了,返回值多种多样 单选或多选按钮消息(前缀:pbm_bm) pbm_bmgetchec ...
- SPA项目首页导航+左侧菜单
Mock.js是个啥 前后端分离之后,前端迫切需要一种机制,不再需要依赖后端接口开发,而今天的主角mockjs就可以做到这一点 Mock.js是一个模拟数据的生成器,用来帮助前端调试开发.进行前后端的 ...
- java实现HTTP请求 HttpUtil
示例: package com.sensor.utils; import java.net.HttpURLConnection; import java.net.URL; public class H ...
- springboot项目实用代码整理
// 判断JSONOBJECT是否为空 CommonUtils.checkJSONObjectIsEmpty(storeInfo) // 判断字符串是否为空," "也为空 Stri ...
- C#使用任务并行库(TPL)
TPL(Task Parallel Library) 任务并行库 (TPL) 是 System.Threading和 System.Threading.Tasks 命名空间中的一组公共类型和 API. ...
- 【转载】Sqlserver使用Convert函数进行数据类型转换
在Sqlserver数据库中,可以使用Convert函数来进行数据类型的转换,如将数字类型decimal转换为字符串nvarchar类型,或者将字符串类型转换为数字类型都可以使用Convert函数来实 ...
- python简单页面爬虫入门 BeautifulSoup实现
本文可快速搭建爬虫环境,并实现简单页面解析 1.安装 python 下载地址:https://www.python.org/downloads/ 选择对应版本,常用版本有2.7.3.4 安装后,将安装 ...