poj 2689 Prime Distance(区间筛选素数)
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 9944 | Accepted: 2677 |
Description
Input
Output
Sample Input
2 17
14 17
Sample Output
2,3 are closest, 7,11 are most distant.
There are no adjacent primes.
Source
#include <stdio.h>
#include <string.h>
#include <math.h>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N=;
int cnt,p[N+],flag[N+];
void get_prime()
{
int i,j;
for(i=;i<N;i++)
{
if(!flag[i])
p[cnt++]=i;;
for(j=;j<cnt&&p[j]*i<N;j++)
{
flag[i*p[j]]=;
if(i%p[j]==)
break;
}
}
} int a[],pp[];
int main()
{
get_prime();
//printf("%d**%d**\n",p[0],p[1]);
ll l,r,i,j;
while(~scanf("%lld%lld",&l,&r))
{
//if(l>r)swap(l,r);
if(l<)l=;
for(i=;i<=r-l;i++)a[i]=;
ll sum=r-l+;//printf("*****\n");
for(i=;a[i]<=r&&i<cnt;i++)
for(j=l/p[i]*p[i];j<=r;j+=p[i])
{
if(j>=l&&j/p[i]>&&a[j-l])
a[j-l]=,sum--;
} if(sum<){printf("There are no adjacent primes.\n");continue;} ll cp=;
for(i=;i<=r-l;i++)
if(a[i]) pp[cp++]=i+l;
ll max,min,pos1,pos2;
max=min=pp[]-pp[];
pos1=pos2=;
for(i=;i<cp;i++)
{
if(max<pp[i]-pp[i-])
{
max=pp[i]-pp[i-];
pos1=i;
}
if(min>pp[i]-pp[i-])
{
min=pp[i]-pp[i-];
pos2=i;
}
}
printf("%d,%d are closest, %d,%d are most distant.\n",pp[pos2-],pp[pos2],pp[pos1-],pp[pos1]) ;
}
return ;
}
poj 2689 Prime Distance(区间筛选素数)的更多相关文章
- POJ - 2689 Prime Distance (区间筛)
题意:求[L,R]中差值最小和最大的相邻素数(区间长度不超过1e6). 由于非素数$n$必然能被一个不超过$\sqrt n$的素数筛掉,因此首先筛出$[1,\sqrt R]$中的全部素数,然后用这些素 ...
- poj 2689 Prime Distance(大区间素数)
题目链接:poj 2689 Prime Distance 题意: 给你一个很大的区间(区间差不超过100w),让你找出这个区间的相邻最大和最小的两对素数 题解: 正向去找这个区间的素数会超时,我们考虑 ...
- poj 2689 Prime Distance (素数二次筛法)
2689 -- Prime Distance 没怎么研究过数论,还是今天才知道有素数二次筛法这样的东西. 题意是,要求求出给定区间内相邻两个素数的最大和最小差. 二次筛法的意思其实就是先将1~sqrt ...
- [ACM] POJ 2689 Prime Distance (筛选范围大素数)
Prime Distance Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12811 Accepted: 3420 D ...
- 数论 - 素数的运用 --- poj 2689 : Prime Distance
Prime Distance Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12512 Accepted: 3340 D ...
- POJ 2689.Prime Distance-区间筛素数
最近改自己的错误代码改到要上天,心累. 这是迄今为止写的最心累的博客. Prime Distance Time Limit: 1000MS Memory Limit: 65536K Total S ...
- POJ 2689 Prime Distance (素数筛选法,大区间筛选)
题意:给出一个区间[L,U],找出区间里相邻的距离最近的两个素数和距离最远的两个素数. 用素数筛选法.所有小于U的数,如果是合数,必定是某个因子(2到sqrt(U)间的素数)的倍数.由于sqrt(U) ...
- poj 2689 Prime Distance(大区间筛素数)
http://poj.org/problem?id=2689 题意:给出一个大区间[L,U],分别求出该区间内连续的相差最小和相差最大的素数对. 由于L<U<=2147483647,直接筛 ...
- POJ 2689 Prime Distance (素数+两次筛选)
题目地址:http://poj.org/problem?id=2689 题意:给你一个不超过1000000的区间L-R,要你求出区间内相邻素数差的最大最小值,输出相邻素数. AC代码: #includ ...
随机推荐
- YJJ's Salesman
YJJ's Salesman YJJ is a salesman who has traveled through western country. YJJ is always on journey. ...
- Anyhashable打印格式化
NSLog("<LocalContactMatch>: \(bestAttemptContent.userInfo as AnyObject)")
- Qt之zip压缩/解压缩(QuaZIP)
摘要: 简述 QuaZIP是使用Qt/C++对ZLIB进行简单封装的用于压缩及解压缩ZIP的开源库.适用于多种平台,利用它可以很方便的将单个或多个文件打包为zip文件,且打包后的zip文件可以通过其它 ...
- Azure Monitor Kibana configuration always seems to send over SSL
https://github.com/elastic/logstash/issues/10125 https://blogs.cisco.com/security/step-by-step-setup ...
- P1080国王游戏
传送 最大值最小什么的一看就是二分了qwq 然鹅并不知道怎么检查,所以我们换个思路 我们要求出最小的最大值,这肯定和大臣的排列有关,会不会有什么规律? 先看看只有两个大臣的情况 排列:1 2,ans1 ...
- 【C++进阶:STL常见性质】
STL中的常用容器包括:顺序性容器(vector.deque.list).关联容器(map.set).容器适配器(queue.stac) 转载自:https://blog.csdn.net/u0134 ...
- iview在项目中遇到的坑
1.下拉框选中某一项搜索发现总是搜不到,最后发现是选中后选中值后边莫名多了很长的空格,原因很简单,在代码中opction闭合标签和主体没有在一行. 2.iview+vue项目中,用百分比或者displ ...
- base64编解码的另外几个版本
#include "crypto/encode/base64.h" static const std::string base64_chars = "ABCDEFGHIJ ...
- C#模块初始化注入
这个功能可以实现很多很有用的功能,比如程序集加密,Hook安装等.英文转载备忘. 原地址:https://www.coengoedegebure.com/module-initializers-i ...
- Zotero引用文献格式(软件学报)
最近在写一篇综述,要处理大量引用文献,选用Zotero作为文献管理工具.在插入参考文献目录时需要遵循格式,奈何网上找不到<软件学报>对应的csl模板文件,所以决定自己动手修改.在此记录下自 ...