2689 -- Prime Distance

  没怎么研究过数论,还是今天才知道有素数二次筛法这样的东西。

  题意是,要求求出给定区间内相邻两个素数的最大和最小差。

  二次筛法的意思其实就是先将1~sqrt(b)内的素数先筛出来,然后再对[a,b]区间用那些筛出来的素数再次线性筛。

代码如下:

 #include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream> using namespace std; const int N = ;
int prm[N >> ], pn;
bool np[N]; void getbase() {
np[pn = ] = np[] = true;
prm[pn++] = ;
for (int i = ; i < N; i++, i++) {
if (!np[i]) prm[pn++] = i;
for (int j = , tmp; j < pn; j++) {
tmp = i * prm[j];
if (tmp >= N) break;
np[tmp] = true;
if (i % prm[j] == ) break;
}
}
// cout << pn << endl;
// for (int i = 0; i < 10; i++) cout << prm[i] << endl;
} bool mk[]; int main() {
// freopen("in", "r", stdin);
getbase();
int T;
long long a, b;
while (~scanf("%lld%lld", &a, &b)) {
a = max(2ll, a);
memset(mk, , sizeof(mk));
for (int i = ; i < pn && prm[i] <= b; i++) {
long long t = a / prm[i] * prm[i];
if (t != a) t += prm[i];
t = max(t, (long long) prm[i] << );
for ( ; t <= b; t += prm[i]) mk[t - a] = true;
}
long long p = a;
int mini = , maxi = -, minp, maxp;
while (p <= b && mk[p - a]) p++;
for (long long i = p + ; i <= b; i++) {
if (mk[i - a]) continue;
// cout << i << endl;
if (maxi < i - p) maxi = i - p, maxp = p;
if (mini > i - p) mini = i - p, minp = p;
p = i;
}
if (maxi < ) puts("There are no adjacent primes.");
else printf("%d,%d are closest, %d,%d are most distant.\n", minp, minp + mini, maxp, maxp + maxi);
}
return ;
}

——written by Lyon

poj 2689 Prime Distance (素数二次筛法)的更多相关文章

  1. poj 2689 Prime Distance(大区间素数)

    题目链接:poj 2689 Prime Distance 题意: 给你一个很大的区间(区间差不超过100w),让你找出这个区间的相邻最大和最小的两对素数 题解: 正向去找这个区间的素数会超时,我们考虑 ...

  2. 数论 - 素数的运用 --- poj 2689 : Prime Distance

    Prime Distance Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12512   Accepted: 3340 D ...

  3. [ACM] POJ 2689 Prime Distance (筛选范围大素数)

    Prime Distance Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12811   Accepted: 3420 D ...

  4. poj 2689 Prime Distance(区间筛选素数)

    Prime Distance Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9944   Accepted: 2677 De ...

  5. POJ 2689 Prime Distance (素数+两次筛选)

    题目地址:http://poj.org/problem?id=2689 题意:给你一个不超过1000000的区间L-R,要你求出区间内相邻素数差的最大最小值,输出相邻素数. AC代码: #includ ...

  6. 题解报告:poj 2689 Prime Distance(区间素数筛)

    Description The branch of mathematics called number theory is about properties of numbers. One of th ...

  7. poj 2689 Prime Distance(大区间筛素数)

    http://poj.org/problem?id=2689 题意:给出一个大区间[L,U],分别求出该区间内连续的相差最小和相差最大的素数对. 由于L<U<=2147483647,直接筛 ...

  8. POJ 2689 Prime Distance(素数筛选)

    题目链接:http://poj.org/problem?id=2689 题意:给出一个区间[L, R],找出区间内相连的,距离最近和距离最远的两个素数对.其中(1<=L<R<=2,1 ...

  9. POJ 2689 Prime Distance (素数筛选法,大区间筛选)

    题意:给出一个区间[L,U],找出区间里相邻的距离最近的两个素数和距离最远的两个素数. 用素数筛选法.所有小于U的数,如果是合数,必定是某个因子(2到sqrt(U)间的素数)的倍数.由于sqrt(U) ...

随机推荐

  1. python实例 输出字符串和数字

    但有趣的是,在javascript里我们会理想当然的将字符串和数字连接,因为是动态语言嘛.但在Python里有点诡异,如下: #! /usr/bin/python a=2 b="test&q ...

  2. Struts_改写客户列表练习

    1.CustomerAction修改放入ActionContext 2.list.jsp使用struts标签库

  3. 洛谷P2327 [SCOI2005]扫雷 [2017年5月计划 清北学堂51精英班Day1]

    P2327 [SCOI2005]扫雷 题目描述 输入输出格式 输入格式: 第一行为N,第二行有N个数,依次为第二列的格子中的数.(1<= N <= 10000) 输出格式: 一个数,即第一 ...

  4. IntelliJ IDEA中设置同时打开多个文件且分行显示

    [转载]原文链接:https://blog.csdn.net/lwl20140904/article/details/73275897 有时候要是打开多个文件,要么都显示在一行,要么因为空间不足,就给 ...

  5. Vue Router 相关

    1. 路由传参: 编程式的导航 router.push this.$router.push("home"); this.$router.push({ name: 'news', p ...

  6. 八.DBN深度置信网络

    BP神经网络是1968年由Rumelhart和Mcclelland为首的科学家提出的概念,是一种按照误差反向传播算法进行训练的多层前馈神经网络,是目前应用比较广泛的一种神经网络结构.BP网络神经网络由 ...

  7. (转)Cookie存中文乱码的问题

    有个奇怪的问题:登录页面中使用Cookie存值,Cookie中要存中文汉字.代码在本地调试,一切OK,汉字也能顺利存到Cookie和从Cookie中读出,但是放到服务器上不管用了,好好的汉字成了乱码, ...

  8. python 下载安装及运行环境配置(windows)

    第一步:下载python安装包 下载地址:https://www.python.org/downloads/windows/ 我下载的是版本:3.6.4 安装包下载完成后,点击进行安装. 第二步:配置 ...

  9. 手把手教你实现一个通用的jsonp跨域方法

    什么是jsonp JSONP(JSON with Padding)是JSON的一种"使用模式",可用于解决主流浏览器的跨域数据访问的问题.由于同源策略,一般来说位于 server1 ...

  10. QLabel添加Click信号

    使用自定义label来实现此功能 其他控件可参照此例. #include "customerqlabel.h" CustomerQlabel::CustomerQlabel(QWi ...