POJ 2689 Prime Distance (素数筛选法,大区间筛选)
题意:给出一个区间[L,U],找出区间里相邻的距离最近的两个素数和距离最远的两个素数。
用素数筛选法。
所有小于U的数,如果是合数,必定是某个因子(2到sqrt(U)间的素数)的倍数。
由于sqrt(U)最大也只有2^16,所以我们可以用素数筛选法,先预处理出2~2^16之间的素数,然后再用这些素数筛选出L~U之间的素数。
接着就好办了。
有几个要注意的是:
1:L为1的情况,可以通过令L=2或者标记isp[0]=false。
2:建议用long long,否则很容易在过程中超int范围,导致数组越界RE。。。
#include <iostream>
#include <cstdio>
#include <string.h> using namespace std;
const int maxn=(<<)+;
const int range=+;
bool isprime[maxn]; //标记2~2^16之间的素数
bool isp[range]; //标记L~U之间的素数,下标从0开始,对应L。
int prime[maxn]; //存储2~2^16之间的素数 int idx;
long long L,U; void init(){
idx=;
memset(isprime,true,sizeof(isprime));
for(int i=;i<maxn;i++){
if(isprime[i]){
prime[idx++]=i;
for(int j=*i;j<maxn;j+=i)
isprime[j]=false;
}
}
}
int main()
{
init();
while(scanf("%I64d%I64d",&L,&U)!=EOF){
memset(isp,true,sizeof(isp));
if(L==)
isp[]=false; //忽略L为1了,导致测试样例1 2的时候,输出结果是1和2最近,1和2最远。
//筛选出L~U之间的素数
for(int i=;i<idx;i++){
int p=prime[i];
/*
这里我RE了好几次,原因如下:
1.j一开始定义成int型,导致j*p有可能超出int范围,变成负数,这样就使得j*p-L<0。
2.L一定要设成long long型,因为l+p-1可能会超int,变成负数。。。
*/
//这里的j表示的是p的倍数,j=(L+p-1)/p表示的是第一个大于等于L的倍数
for(long long j=(L+p-)/p;j*p<=U;j++){
if(j>){
isp[j*p-L]=false;
}
}
}
int mina,minb,mindis=,maxa,maxb,maxdis=;
int a,b;
bool first=true;
for(int i=;i<=U-L;i++){
if(isp[i]){
if(!first){
b=i;
if(b-a<mindis){
mina=a;
minb=b;
mindis=b-a;
}
if(b-a>maxdis){
maxa=a;
maxb=b;
maxdis=b-a;
}
a=b;
}
else{
first=false;
a=i;
}
}
}
if(!maxdis){
printf("There are no adjacent primes.\n");
}
else{
//注意:这里输出时long long格式,因为L是long long型
printf("%I64d,%I64d are closest, %I64d,%I64d are most distant.\n",mina+L,minb+L,maxa+L,maxb+L);
}
}
return ;
}
POJ 2689 Prime Distance (素数筛选法,大区间筛选)的更多相关文章
- poj 2689 Prime Distance(大区间素数)
题目链接:poj 2689 Prime Distance 题意: 给你一个很大的区间(区间差不超过100w),让你找出这个区间的相邻最大和最小的两对素数 题解: 正向去找这个区间的素数会超时,我们考虑 ...
- poj 2689 Prime Distance(大区间筛素数)
http://poj.org/problem?id=2689 题意:给出一个大区间[L,U],分别求出该区间内连续的相差最小和相差最大的素数对. 由于L<U<=2147483647,直接筛 ...
- 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(素数筛选)
题目链接:http://poj.org/problem?id=2689 题意:给出一个区间[L, R],找出区间内相连的,距离最近和距离最远的两个素数对.其中(1<=L<R<=2,1 ...
- 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 ...
- POJ 2689 Prime Distance (素数+两次筛选)
题目地址:http://poj.org/problem?id=2689 题意:给你一个不超过1000000的区间L-R,要你求出区间内相邻素数差的最大最小值,输出相邻素数. AC代码: #includ ...
- 题解报告:poj 2689 Prime Distance(区间素数筛)
Description The branch of mathematics called number theory is about properties of numbers. One of th ...
随机推荐
- ruby on rails 实战(一)
通过ruby on rails 一步一步搭建个人站点,涉及到的技术有:ruby,rails,javascript,jquery 操作系统:win7 IDE: rubymine 5.4. 第一步,下载安 ...
- SOCKET 地址
地址格式: 函数bind和getsockname使用通用数据类型:struct sockaddr*来指向socket地址. #incude <sys/socket.h> struct so ...
- LN : leetcode 292 Nim Game
lc 292 Nim Game 292 Nim Game You are playing the following Nim Game with your friend: There is a hea ...
- Java从入门到精通——调错篇之SVN 出现 Loced错误
我在更新SVN的时候同时并打开了在这SVN上的一个文档结果导致了我更新的时候提示Loced错误 解决方法:出现这个问题后使用"清理"功能,如果还不行,就直接到上一级目录,再执行&q ...
- java程序用做windows服务
具体步骤在这里 http://www.doc88.com/p-360144091164.html 遇到错误: JVM did not exit on request, terminated 通过下面的 ...
- html a 链接
1.链接元素 文本.图像.热区 2.标记 描述性文字 target窗口形式 _self:在自身窗口打开(默认) _blank:在新窗口打开 _parent:在父窗口打开 _top:在顶窗口打开 框架或 ...
- Java Day 11
异常 Throwable 子类 Error.Exception Error - 不可以处理 Exception - 针对性处理 原理 自定义异常 异常类的抛出throws 先检查语法错误,后检查逻辑 ...
- 0-N背包为题(动态规划算法)
/****************0-N背包问题****************** * 有n个物体装入容量为c的背包,每一个物体有一个体积 * 和一个价值,所装入的物体体积之和不大于背包体积, * ...
- java字符串替换函数高效实现
public static String removeStr(String src, String str) { if (src == null || str == null) return src; ...
- Differences Between Xcode Project Templates for iOS Apps
Differences Between Xcode Project Templates for iOS Apps When you create a new iOS app project in Xc ...