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 ...
随机推荐
- 函数的光滑化或正则化 卷积 应用 两个统计独立变量X与Y的和的概率密度函数是X与Y的概率密度函数的卷积
http://graphics.stanford.edu/courses/cs178/applets/convolution.html Convolution is an operation on t ...
- 【python】-- json & pickle、xml、requests、hashlib、shelve、shutil、configparser、subprocess
json & pickle Python中用于序列化的两个模块 json 用于[字符串]和 [python基本数据类型] 间进行转换 pickle 用于[python特有的类型] ...
- SAP HR 复制PA30的人员
[转自http://www.512test.com/home/space.php?uid=19&do=blog&id=2381] 很多顾问测试HR的程序时都为录入人员头痛,下面的程序提 ...
- oss2模块和aliyun oss链接
安装oss pip install oss2 首先已经理解OSS 基本概念,如Bucket.Object.Endpoint.AccessKeyId和AccessKeySecret等. 下面介绍如何使用 ...
- pandas to_datetime()
>>> import pandas as pd >>> i = pd.date_range() >>> df = pd.DataFrame(dic ...
- Python 3 并发编程多进程之队列(推荐使用)
Python 3 并发编程多进程之队列(推荐使用) 进程彼此之间互相隔离,要实现进程间通信(IPC),multiprocessing模块支持两种形式:队列和管道,这两种方式都是使用消息传递的. 可以往 ...
- 收缩VC数据库
注意: 在收缩日志前必须截断事务日志. 一. SQL Server 2008 收缩日志 (1) 使用SQL管理器收缩日志 第一步执行如下命令 ALTER DATABASE dbname SET REC ...
- Win7打开新的文件夹总会以新窗口的形式打开
首先可以在 组织-->文件夹和搜索选项 中设置“在同一窗口中打开每个文件夹” 如果设置后不起作用还可以 管理员方式执行以下两条命令 在开始菜单-运行中输入regsvr32 "%Sy ...
- EntityFramework 学习 一 Entity Relationships 实体的关系
下面,我们学习Entity Framework怎么管理实体间的关系 Entity Framework支持三种关系:一对一的关系.一对多的关系.多对多的关系 前面我们创建SchoolDB的实体数据模型, ...
- java原生数据类型和引用类型
java 中String 是个对象,是引用类型基础类型与引用类型的区别是,基础类型只表示简单的字符或数字,引用类型可以是任何复杂的数据结构基本类型仅表示简单的数据类型,引用类型可以表示复杂的数据类型, ...