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 ...
随机推荐
- 如何减小ios安装包大小
以前的老文章了,搬到cnblog 更小的安装包意味着更快的下载安装速度,也往往意味着更快的加载运行速度,是优化ios应用的一个重要方面,本文主要参考<减小iOS应用程序的大小>,在实际测试 ...
- 从PRISM开始学WPF(六)MVVM(二)Command?
从PRISM开始学WPF(一)WPF? 从PRISM开始学WPF(二)Prism? 从PRISM开始学WPF(三)Prism-Region? 从PRISM开始学WPF(四)Prism-Module? ...
- JAVA_SE基础——57.有了包之后类与类之间的访问使用import语句
代码1访问代码2 代码1: class Demo3 { public static void main(String[] args) { Demo4 a = new Demo4(); a.print( ...
- JavaScript AJAX实例
原生JS实现AJAX: // method : 请求方式 POST/GET; // url: 如果为GET方式的话url里面要带参数 // obj: 准备好的容器,方便储存拿到的数据 function ...
- python 面向对象之多态与绑定方法
多态与多态性 一,多态 1,多态指的是一类事物有多种形态(python里面原生多态) 1.1动物有多种形态:人,狗,猪 import abc class Animal(metaclass=abc.AB ...
- 批量检测GoAhead系列服务器中Digest认证方式的服务器弱口令
最近在学习用python写爬虫工具,某天偶然发现GoAhead系列服务器的登录方式跟大多数网站不一样,不是采用POST等方法,通过查找资料发现GoAhead是一个开源(商业许可).简单.轻巧.功能强大 ...
- GridControl的常用操作
1.GridView的回车跳转单元格和换行 private void gridView1_KeyPress(object sender, KeyPressEventArgs e) { ...
- Django ORM那些相关操作
一般操作 https://docs.djangoproject.com/en/1.11/ref/models/querysets/ 官网文档 常用的操作 <1> all() ...
- 日推20单词 Day02
1.distinguish v. 区别,辨别 2.tension n. 紧张,不安 3.sympathy n. 同情,慰问 4.admiration n. 羡慕 5.jealousy n. 嫉妒 6. ...
- JVM 性能调优监控工具
声明:本文转自<https://www.cnblogs.com/anxiao/p/6796644.html?utm_source=itdadao&utm_medium=referral& ...