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. Apache HttpComponents 工具类 [ HttpUtil ]

    pom.xml <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId&g ...

  2. 2018-8-10-如何移动-nuget-缓存文件夹

    title author date CreateTime categories 如何移动 nuget 缓存文件夹 lindexi 2018-08-10 19:16:51 +0800 2018-2-13 ...

  3. 洛谷P1966 [NOIP2013提高组Day1T2]火柴排队

    P1966 火柴排队 题目描述 涵涵有两盒火柴,每盒装有 n 根火柴,每根火柴都有一个高度. 现在将每盒中的火柴各自排成一列, 同一列火柴的高度互不相同, 两列火柴之间的距离定义为: ∑(ai-bi) ...

  4. 获取电脑名和IP地址

    private string  GetHostNameAndIP( bool  isv4Orv6)        {            string HostName = Dns.GetHostN ...

  5. java如何访问memcache

    1       Memcache是什么 Memcache是danga.com的一个项目,最早是为 LiveJournal 服务的,目前全世界不少人使用这个缓存项目来构建自己大负载的网站,来分担数据库的 ...

  6. Leetcode867.Transpose Matrix转置矩阵

    给定一个矩阵 A, 返回 A 的转置矩阵. 矩阵的转置是指将矩阵的主对角线翻转,交换矩阵的行索引与列索引. 示例 1: 输入:[[1,2,3],[4,5,6],[7,8,9]] 输出:[[1,4,7] ...

  7. 二.python数据结构的性能分析

    目录: 1.引言 2.列表 3.字典 一.引言 - 现在大家对 大O 算法和不同函数之间的差异有了了解.本节的目标是告诉你 Python 列表和字典操作的 大O 性能.然后我们将做一些基于时间的实验来 ...

  8. hackerrank---List Comprehensions

    题目链接 刷刷Python基本功...列表解析 附上代码: x = int(input()) y = int(input()) z = int(input()) n = int(input()) pr ...

  9. 百度语音识别REST API用法(含JAVA代码)——不须要集成SDK的方法

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/zpf8861/article/details/32329457 上一篇文章http://blog.c ...

  10. python 内置函数补充 or 递归 or 二分法

    一.内置函数的补充 repr() 显示出字符串的官方表示形式 chr() print(chr(20013)) # 把数字编码转换成字符串 ord() print(ord('中')) # 20013 把 ...