POJ2689:素数区间筛选
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 15820 | Accepted: 4202 |
Description
Your program is given 2 numbers: L and U (1<=L< U<=2,147,483,647), and you are to find the two adjacent primes C1 and C2 (L<=C1< C2<=U) that are closest (i.e. C2-C1 is the minimum). If there are other pairs that are the same distance apart, use the first pair. You are also to find the two adjacent primes D1 and D2 (L<=D1< D2<=U) where D1 and D2 are as distant from each other as possible (again choosing the first pair if there is a tie).
Input
Output
Sample Input
2 17
14 17
Sample Output
2,3 are closest, 7,11 are most distant.
There are no adjacent primes.
注意:L可能为1
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int MAXN = ;
typedef long long LL;
LL l, u;
bool isPrime[MAXN], isSmallPrime[MAXN];
int prime[MAXN], len;
void prep()
{
memset(isPrime, true, sizeof(isPrime));
memset(isSmallPrime, true, sizeof(isSmallPrime));
isSmallPrime[] = false;
isSmallPrime[] = false;
len = ;
for(LL i = ; i * i <= u; i++)
{
if(isSmallPrime[i])
{
for(LL j = i + i; j * j <= u; j += i)
{
isSmallPrime[j] = false;
}
for(LL j = max(i + i, (l + i - ) / i * i); j <= u; j += i)
{
isPrime[j-l] = false;
}
}
}
for(LL i = l; i <= u; i++)
{
if(isPrime[i-l])
{
prime[len++] = i;
}
}
}
int main()
{
while(scanf("%I64d %I64d", &l, &u) != EOF)
{
if(l == ) l++;
prep();
if(len <= )
{
printf("There are no adjacent primes.\n");
continue;
}
int mind = 0x3f3f3f3f, a, b;
int maxd = , c, e;
for(int i = ; i < len; i++)
{
int d = prime[i] - prime[i-];
if(mind > d)
{
mind = d;
a = prime[i-];
b = prime[i];
}
if(maxd < d)
{
maxd = d;
c = prime[i-];
e = prime[i];
}
}
printf("%d,%d are closest, %d,%d are most distant.\n", a, b, c, e);
}
return ;
}
POJ2689:素数区间筛选的更多相关文章
- poj2689(素数区间筛法模板)
题目链接: http://poj.org/problem?id=2689 题意: 给出一个区间 [l, r] 求其中相邻的距离最近和最远的素数对 . 其中 1 <= l < r < ...
- POJ-2689-Prime Distance(素数区间筛法)
链接: https://vjudge.net/problem/POJ-2689 题意: The branch of mathematics called number theory is about ...
- HDOJ/HDU 2710 Max Factor(素数快速筛选~)
Problem Description To improve the organization of his farm, Farmer John labels each of his N (1 < ...
- POJ 2689 Prime Distance (素数筛选法,大区间筛选)
题意:给出一个区间[L,U],找出区间里相邻的距离最近的两个素数和距离最远的两个素数. 用素数筛选法.所有小于U的数,如果是合数,必定是某个因子(2到sqrt(U)间的素数)的倍数.由于sqrt(U) ...
- poj2689 Prime Distance(素数区间筛法)
题目链接:http://poj.org/problem?id=2689 题目大意:输入两个数L和U(1<=L<U<=2 147 483 647),要找出两个相邻素数C1和C2(L&l ...
- poj 2689 Prime Distance(区间筛选素数)
Prime Distance Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9944 Accepted: 2677 De ...
- HDU 2136 Largest prime factor(查找素数,筛选法)
题目梗概:求1000000以内任意数的最大质因数是第几个素数,其中 定义 1为第0个,2为第1个,以此类推. #include<string.h> #include<stdio.h& ...
- HDOJ(HDU) 2138 How many prime numbers(素数-快速筛选没用上、)
Problem Description Give you a lot of positive integers, just to find out how many prime numbers the ...
- hdu Diophantus of Alexandria(素数的筛选+分解)
Description Diophantus of Alexandria was an egypt mathematician living in Alexandria. He was one of ...
随机推荐
- linux c编程:进程环境
一 进程终止: ⼀个进程可以登记若⼲个(具体⾃⼰验证⼀下)个函数,这些函数由exit⾃动调⽤,这些函数被称为终⽌处理函数, atexit函数可以登记这些函数. exit调⽤终⽌处理函数的顺序和atex ...
- QT5使用Webkti
Qt 5.3 使用原来的QT4.8.4项目时QWebView .QWebFrame等类无法编译通过. 出现原因:QWebView .QWebFrame.QWebPage.QWebInspector等这 ...
- 【SHARE】WEB前端学习资料
参考资料:https://github.com/karlhorky/learn-to-program 学习网站:http://www.codecademy.com/learn https://www. ...
- TCP标准模板
伪代码 #创建一个TCP服务器 ss = socket() #创建服务器套接字 ss.bind() #把地址绑定到套接字上 ss.listen() #监听连接 inf_loop: #服务器无线循环 c ...
- LeetCode:寻找重复数【287】
LeetCode:寻找重复数[287] 题目描述 给定一个包含 n + 1 个整数的数组 nums,其数字都在 1 到 n 之间(包括 1 和 n),可知至少存在一个重复的整数.假设只有一个重复的整数 ...
- Ubuntu安装SSH + Windows上配置Putty
1. Ubuntu安装SSH 命令: # sudo apt-get install openssh-server 2. 启动SSH Server 命令: # sudo /etc/init.d/ssh ...
- Data Structure Graph: strong connectivity
如果为undirected graph就是dfs或者bfs,如果都能visit则为连通O(V+E). 如果为directed graph就先dfs或者bfs,再reverse direct,再dfs或 ...
- leetcode 900. RLE Iterator
Write an iterator that iterates through a run-length encoded sequence. The iterator is initialized b ...
- P3506 [POI2010]MOT-Monotonicity 2
题目 P3506 [POI2010]MOT-Monotonicity 2 第一次切掉没题解的题\(qwq\) 做法 首先确定\(a_i\)的位置后显然就能确定\(a_{i+1}\)的位置,建一棵权值线 ...
- 剑指offer之 奇数偶数数组位置调整且保存顺序不变
public class Solution { public void reOrderArray(int [] array) { reOrderCore(array,array.length); } ...