poj 2689 (素数二次筛选)
Sample Input
2 17
14 17
Sample Output
2,3 are closest, 7,11 are most distant.
There are no adjacent primes.
找出给定范围内,距离最远和最近的素数。(不停超时 - -)
给的上界很大,所以全处理肯定不行。 先处理sqrt(2147483647)。
然后再在l 与 r之间筛选素数。
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
#define N 10100
typedef long long ll;
using namespace std;
int n,m;
int f[1000500];
int q[1000500];
int t[100050];
int tot;
void prim()
{
ll i,j;
for(i = 2; i <= 100050; i++)
q[i] = 1;
for(i= 2,tot = 0; i <= 100050; i++)
{
if(q[i])
{
t[tot++] = i;
for(j = i*2; j <= 100050; j+=i)
q[j] = 0;
}
}
} int main()
{
int l,r;
while(scanf("%d%d",&l,&r)!= EOF)
{
prim();
if(l == 1)
l = 2; memset(f,0,sizeof(f));
for(int i =0; i < tot; i++)
{
int a = (l-1)/t[i]+1;
int b = r /t[i];
for(int j = a; j <= b; j++) //类似上面prim()的方法
if(j > 1)
f[j*t[i]-l] = 1;
}
int Max = -1;
int Min = 1000000000;
int tmp = -1;
int maxl,maxr,minl,minr; for(int i = 0; i <= r-l; i++)
{
if(!f[i])
{
if(tmp>=0 && i - tmp> Max)
{
Max = i-tmp;
maxl = tmp+l;
maxr = i+l;
}
if(tmp>=0 && i - tmp< Min)
{
Min = i-tmp;
minl = tmp+l;
minr = i+l;
}
tmp = i;
}
}
if(Max == -1)
printf("There are no adjacent primes.\n");
else
{
printf("%d,%d are closest, %d,%d are most distant.\n",minl,minr,maxl,maxr);
}
}
return 0;
}
poj 2689 (素数二次筛选)的更多相关文章
- CodeForces114E——Double Happiness(素数二次筛选)
Double Happiness On the math lesson a teacher asked each pupil to come up with his own lucky numbers ...
- poj 2689 Prime Distance(区间筛选素数)
Prime Distance Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9944 Accepted: 2677 De ...
- 大区间素数筛选(POJ 2689)
/* *POJ 2689 Prime Distance *给出一个区间[L,U],找出区间内容.相邻的距离最近的两个素数和距离最远的两个素数 *1<=L<U<=2147483647 ...
- poj 2689 Prime Distance (素数二次筛法)
2689 -- Prime Distance 没怎么研究过数论,还是今天才知道有素数二次筛法这样的东西. 题意是,要求求出给定区间内相邻两个素数的最大和最小差. 二次筛法的意思其实就是先将1~sqrt ...
- 数学#素数筛法 HDU 4548&POJ 2689
找素数本来是很简单的问题,但当数据变大时,用朴素思想来找素数想必是会超时的,所以用素数筛法. 素数筛法 打表伪代码(用prime数组保存区间内的所有素数): void isPrime() vis[]数 ...
- poj 2689 Prime Distance(大区间素数)
题目链接:poj 2689 Prime Distance 题意: 给你一个很大的区间(区间差不超过100w),让你找出这个区间的相邻最大和最小的两对素数 题解: 正向去找这个区间的素数会超时,我们考虑 ...
- MySQL-分组查询(GROUP BY)及二次筛选(HAVING)
为了测试GROUP BY 语句,我们创建两张表,并往表中添加数据 -- 创建部门表 CREATE TABLE IF NOT EXISTS department( id TINYINT UNSIGNED ...
- MYSQL 二次筛选,统计,最大值,最小值,分组,靠拢
HAVING 筛选后再 筛选 SELECT CLASS,SUM(TOTAL_SCORES) FROM student_score GROUP BY CLASS HAVING SUM(TOTAL_SCO ...
- POJ 2689 Prime Distance(素数筛选)
题目链接:http://poj.org/problem?id=2689 题意:给出一个区间[L, R],找出区间内相连的,距离最近和距离最远的两个素数对.其中(1<=L<R<=2,1 ...
随机推荐
- c语言博客第二次作业
一.PTA实验作业 题目1:计算分段函数[2] 1.实验代码 { double x,y; scanf("%lf",&x); if(x>=0) { y=pow(x,0. ...
- c/cpp语言链表连接部分详解
核心代码: ①pTail->next = pNew; ②pNew->next = NULL; ③pTail = pNew; 设结构体名称为 struct ST: 注:方框代表分配的内存空间 ...
- day-3 python多线程编程知识点汇总
python语言以容易入门,适合应用开发,编程简洁,第三方库多等等诸多优点,并吸引广大编程爱好者.但是也存在一个被熟知的性能瓶颈:python解释器引入GIL锁以后,多CPU场景下,也不再是并行方式运 ...
- IIS 配置 FTP 网站
在 服务器管理器 的 Web服务器IIS 上安装 FTP 服务 在 IIS管理器 添加FTP网站 配置防火墙规则 说明:服务器环境是Windows Server 2008 R2,IIS7.5. 1. ...
- 优化从 App.config 读取配置文件
public class AppSettingsConfig { /// <summary> ////// </summary> public static int Query ...
- HTTP请求到爬虫代码的终南捷径
前阵子在做爬虫的时候学会了各种抓包,看到http请求的时候硬拼代码实在有点累. 后来发现Postman工具是直接可以把Postman请求直接生成对应的代码,这样一下来就美滋滋了. 那么最后的问题就成了 ...
- Python内置函数(26)——enumerate
英文文档: enumerate(iterable, start=0) Return an enumerate object. iterable must be a sequence, an itera ...
- ssh_maven的搭建之dao层的开发
之前都是使用我们传统的方式进行引入jar包,现在我们使用maven进行管理依赖,这样,我们的jar就不需要我们进行管理,而且,我们的maven还可以进行项目构建,一个项目从编写源代码到编译,测试,运行 ...
- python中的turtle库绘制图形
1. 前奏: 在用turtle绘制图形时,需要安装对应python的解释器以及IDE,我安装的是pycharm,在安装完pycharm后,在pycharm安装相应库的模块,绘图可以引入turtle模块 ...
- python Django之Ajax
python Django之Ajax AJAX,Asynchronous JavaScript and XML (异步的JavaScript和XML),一种创建交互式网页应用的网页开发技术方案. 异步 ...