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 ...
随机推荐
- Linux7 安装python3.5.4
1.首先修改yum配置文件 因为yum使用python2,因此替换为python3后可能无法正常工作,继续使用这个python2.7.5 因此修改yum配置文件(vi /usr/bin/yum). 把 ...
- Python13之元组(带上枷锁的列表)
一.元组定义 元组一旦建立,元组内的元素不允许修改和删除,这就是元组和列表最大的区别 当元组中仅有一个元素时,需要将元素后面加上逗号,或者不用括号也可以. tuple1 = (12,3234,5435 ...
- Python中遍历整个列表及注意点(参考书籍Python编程从入门到实践)
1. 利用for循环遍历整个列表 magicians = ['alice', 'dsvid', 'carolina'] # 遍历整个列表 for magician in magicians: prin ...
- springboot基础、注解等
SpringBoot 1.springboot概念 Spring Boot是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置. ...
- Netty服务端创建流程及组件职责
public class NettyServer { public static void main(String[] args) throws InterruptedException { NioE ...
- netcore使用EFcore(第一个实例)
说明:搭建netcore 使用efcore入门教程,跟着这个教程,傻瓜都可以成功!O(∩_∩)O哈哈~,咱们开始吧: 首先介绍下环境: vs2017, netcore2.2, EntityFramew ...
- C#判断字符串中含有多少个汉字
private void button1_Click(object sender, EventArgs e) { ArrayList itemList = new ArrayList(); CharE ...
- CVPR2014: DeepID解读
上周五就要发的,拖........拖.......拖到现在,文中有不准确的地方,欢迎批评指正. DeepID是一种特征提取的算法,由港中文汤晓鸥团队于2014年提出,发表于CVPR2014.其应用领域 ...
- ubuntu下使用JNI Java调用C++的例子
TestJNI.java public class TestJNI { static{ System.load("/home/buyizhiyou/workspace/JNI/src/lib ...
- Python 使用gevent实现多任务
import gevent import time # 如果需要默认的 time.sleep(0.5) 需要打补丁 from gevent import monkey monkey.patch_all ...