题意:求[a, b]之间差最大/小的相邻素数。

0 < a, b < 2^32, 0 < b - a <= 1e6

首先发现a,b很大,以至于无法求出素数来。

然后就考虑退而求次,求出sqrt(b)以内的素数。

发现可以枚举[a, b]之间的数,还开的下一个vis数组。

然后考虑筛去所有合数。

用sqrt(b)以内的素数筛合数即可。

有两个点要注意:

1,b可能等于2147483647,故for循环里面要写成 i <= b && i > 0

2,注意1不是质数,所以a == 1时vis[0] = 1

 #include <cstring>
#include <cstdio>
typedef long long LL;
const int N = ;
LL INF = ; int a, b, top, p[N];
bool vis[N]; inline void solve() {
memset(vis, , (b - a + ) * sizeof(bool));
for(int i = ; i <= top && p[i] * p[i] <= b; i++) {
int j = (a / p[i]) * p[i];
if(j < a) {
j += p[i];
}
if(j == p[i]) {
j += p[i];
}
for(; j <= b && j > ; j += p[i]) {
vis[j - a] = ;
}
}
if(a == ) {
vis[] = ;
} int last = ;
int c = , d = ;
for(int i = a; i <= b && i > ; i++) {
if(vis[i - a]) {
continue;
}
if(!d) {
d = i;
}
else if(!c) {
c = d;
d = i;
last = d;
}
else {
if(i - last < d - c) {
c = last;
d = i;
}
last = i;
}
}
if(!c) {
puts("There are no adjacent primes.");
return;
}
printf("%d,%d are closest, ", c, d);
last = c = d = ;
for(int i = a; i <= b && i > ; i++) {
if(vis[i - a]) {
continue;
}
if(!d) {
d = i;
}
else if(!c) {
c = d;
d = i;
last = d;
}
else {
if(i - last > d - c) {
c = last;
d = i;
}
last = i;
}
}
printf("%d,%d are most distant.\n", c, d);
return;
} inline void getp() {
for(int i = ; 1ll * i * i <= INF; i++) {
if(!vis[i]) {
p[++top] = i;
}
for(int j = ; j <= top && 1ll * i * p[j] * i * p[j] <= INF; j++) {
vis[i * p[j]] = ;
if(i % p[j] == ) {
break;
}
}
}
return;
} int main() {
getp();
while(scanf("%d%d", &a, &b) != EOF) {
solve();
} return ;
}

AC代码

poj2689 Prime Distance的更多相关文章

  1. POJ2689 Prime Distance(数论:素数筛选模板)

    题目链接:传送门 题目: Prime Distance Time Limit: 1000MS Memory Limit: 65536K Total Submissions: Accepted: Des ...

  2. POJ-2689 Prime Distance (两重筛素数,区间平移)

    Prime Distance Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13961   Accepted: 3725 D ...

  3. 解题报告:poj2689 Prime Distance

    2017-10-03 11:29:20 writer:pprp 来源:kuangbin模板 从已经筛选好的素数中筛选出规定区间的素数 /* *prime DIstance *给出一个区间[L,U],找 ...

  4. POJ-2689 Prime Distance,区间素数筛法

                                                    Prime Distance 只会埃氏筛法的弱鸡今天读了读挑战程序设计120页,明白了求小区间内素数的方 ...

  5. POJ2689:Prime Distance(大数区间素数筛)

    The branch of mathematics called number theory is about properties of numbers. One of the areas that ...

  6. POJ2689 - Prime Distance(素数筛选)

    题目大意 给定两个数L和U,要求你求出在区间[L, U] 内所有素数中,相邻两个素数差值最小的两个素数C1和C2以及相邻两个素数差值最大的两个素数D1和D2,并且L-U<1,000,000 题解 ...

  7. poj2689 Prime Distance题解报告

    题目戳这里 [题目大意] 给定一个区间[L,R],求区间内的质数相邻两个距离最大和最小的. [思路分析] 其实很简单呀,很明显可以看出来是数论题,有关于质数的知识. 要注意一下的就是L和R的数据范围都 ...

  8. POJ2689 Prime Distance 质数筛选

    题目大意 求区间[L, R]中距离最大和最小的两对相邻质数.R<2^31, R-L<1e6. 总体思路 本题数据很大.求sqrt(R)的所有质数,用这些质数乘以j, j+1, j+2... ...

  9. poj2689 Prime Distance(素数区间筛法)

    题目链接:http://poj.org/problem?id=2689 题目大意:输入两个数L和U(1<=L<U<=2 147 483 647),要找出两个相邻素数C1和C2(L&l ...

随机推荐

  1. 【学亮IT手记】使用Map代替switch...case语句

  2. 重启iis命令

    iisreset

  3. python爬虫之git的使用(windows下pycharm使用)

    相信很多同学学会了git或者github以后都不知道怎么跟windows上的pycharm连在一起工作,那么下面我们开始介绍简单的安装和使用方法. 一.安装 1.首先你的有一个github的账户.注册 ...

  4. ubuntu18.04 安装 php7.2

    sudo apt-get install software-properties-common python-software-properties sudo add-apt-repository p ...

  5. Netty ByteBuf和Nio ByteBuffer

    参考https://blog.csdn.net/jeffleo/article/details/69230112 一.简介 Netty中引入了ByteBuf,它相对于ByteBuffer来说,带来了很 ...

  6. mysql 常用字段类型

    tinyint[(m)] [unsigned] [zerofill] 1字节 极小整数,数据类型用于保存一些范围的整数数值范围: 有符号: -128 - 127. 无符号: - 255 特别的: My ...

  7. python数据结构与算法第十一天【希尔排序】

    1.希尔排序的原理 2.代码实现 def shell_sort(alist): n = len(alist) # 初始步长 gap = n / 2 while gap > 0: # 按步长进行插 ...

  8. CS新建排版

    1.拉菜单栏barmanage,去掉不要的头部和尾部  ,选择控件bar属性optionsbar 全部为false,防止菜单拖动. 2.拉一个panelcontrol属性dock 设置顶部,在拉一个p ...

  9. 树&图 记录

    A - Lake Counting POJ - 2386 最最最最最基础的dfs 挂这道题为了提高AC率(糖水不等式 B - Paint it really, really dark gray Cod ...

  10. vuex2.0 基本使用(4) --- modules

    vue 使用的是单一状态树对整个应用的状态进行管理,也就是说,应用中的所有状态都放到store中,如果是一个大型应用,状态非常多, store 就会非常庞大,不太好管理.这时vuex 提供了另外一种方 ...