Prime Distance POJ - 2689 (数学 素数)
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、r区间内,差值最大和最小的相邻质数。
思路:因为n很大,所以不可能直接找出所有的质数后遍历
对于区间1~n,我们只需要找出√n 范围内的素数,倍增标记剩余区间的合数,就可以得到区间的所有质数。
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std; const int maxn = 1e5;
int prime[maxn];
int tot;
void get_pri(int n)
{
bool v[n+];
memset(v,,sizeof(v));
for(int i=; i<=n; i++)
{
if(!v[i])
prime[++tot] = i;
for(int j=i; j<=n/i; j++)
{
v[j*i] = ;
}
}
}
int main()
{ int l,r;
while(~scanf("%d%d",&l,&r))
{
tot = ;
get_pri(sqrt(r));
bool vis[r-l+];
memset(vis,,sizeof(vis));
for(int i=; i<=tot; i++)
{
for(int j=ceil(l*1.0/prime[i]); j<=r/prime[i]; j++)
{
if(j == )continue;
vis[prime[i]*j-l] = ;
}
}
int ans[r-l+];
int cnt = ;
for(int i=; i<=r-l; i++)
{
if(!vis[i])
{
if(i+l == )continue;
ans[++cnt] = i+l;
}
}
if(cnt < )
printf("There are no adjacent primes.\n");
else
{
int minn = 0x3f3f3f3f;
int maxx = ;
int id1;
int id2;
for(int i=; i<cnt; i++)
{
int tmp = ans[i+]-ans[i];
if(tmp < minn)
{
minn = tmp;
id1 = i;
}
if(tmp > maxx)
{
maxx = tmp;
id2 = i;
}
}
printf("%d,%d are closest, %d,%d are most distant.\n",ans[id1],ans[id1+],ans[id2],ans[id2+]);
}
}
}
Prime Distance POJ - 2689 (数学 素数)的更多相关文章
- Prime Distance POJ - 2689 线性筛
一个数 $n$ 必有一个不超过 $\sqrt n$ 的质因子. 打表处理出 $1$ 到 $\sqrt n$ 的质因子后去筛掉属于 $L$ 到 $R$ 区间的素数即可. Code: #include&l ...
- [ACM] POJ 2689 Prime Distance (筛选范围大素数)
Prime Distance Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12811 Accepted: 3420 D ...
- POJ2689 Prime Distance(数论:素数筛选模板)
题目链接:传送门 题目: Prime Distance Time Limit: 1000MS Memory Limit: 65536K Total Submissions: Accepted: Des ...
- poj 2689 Prime Distance(大区间筛素数)
http://poj.org/problem?id=2689 题意:给出一个大区间[L,U],分别求出该区间内连续的相差最小和相差最大的素数对. 由于L<U<=2147483647,直接筛 ...
- POJ2689:Prime Distance(大数区间素数筛)
The branch of mathematics called number theory is about properties of numbers. One of the areas that ...
- poj 2689 区间素数筛
The branch of mathematics called number theory is about properties of numbers. One of the areas that ...
- poj 2689 巧妙地运用素数筛选
称号: 给出一个区间[L,R]求在该区间内的素数最短,最长距离. (R < 2 * 10^9 , R - L <= 10 ^ 6) 由数论知识可得一个数的因子可在开根号内得到. 所以,我们 ...
- poj 2689 (素数二次筛选)
Sample Input 2 17 14 17 Sample Output 2,3 are closest, 7,11 are most distant. There are no adjacent ...
- [POJ268] Prime Distance(素数筛)
/* * 二次筛素数 * POJ268----Prime Distance(数论,素数筛) */ #include<cstdio> #include<vector> using ...
随机推荐
- react创建组件的几种方式及其区别
react创建组件有如下几种方式 ①.函数式定义的无状态组件 ②.es5原生方式React.createClass定义的组件 ③.es6形式的extends React.Component定义的组 ...
- yii2 or查询
// 我们要查询id等于1或者id等于3的数据 $userInfo = User::find()->where(['or' , 'id = 1' , 'id = 3'])->all(); ...
- POM文件分析记
pom英文全称:project object model 1.简介 pom.xml文件描述了maven项目的基本信息,比如groupId,artifactId,version等.也可以对maven项目 ...
- 驱动调试(四)oops确定调用树
目录 驱动调试(四)oops确定调用树 内核开启调用树 栈指针分析 原理 寄存器别名 基础解释 例子分析 找到PC地址的位置 栈分析 附录:原文的excel title: 驱动调试(四)oops确定调 ...
- 第四节:Task的启动的四种方式以及Task、TaskFactory的线程等待和线程延续的解决方案
一. 背景 揭秘: 在前面的章节介绍过,Task出现之前,微软的多线程处理方式有:Thread→ThreadPool→委托的异步调用,虽然也可以基本业务需要的多线程场景,但它们在多个线程的等待处理方面 ...
- 页面加载过渡页 loading plugin css
是前文 plugin 示例 loading 中需要用到的样式.现在样式扩充为 5类.loadtwo 使用到了bgimg /*loading animation loading one*/ /* loa ...
- 使用kqueue的str_cli函数
void str_cli(FILE *fp, int sockfd) { , isfile; char buf[MAXLINE]; ]; struct timespec ts; struct stat ...
- 浏览器录宏重放软件-iMacros
iMacros https://imacros.net/ iMacros v12 Now Available The world's most popular web automation, data ...
- 第28月第24天 requestSerializer
1. requestSerializer关于 requestSerializer它就是AFNetworking参数编码的序列化器,它一共有三种编码格式: AFHTTPRequestSerializer ...
- Django之Cookie和Session
http://www.cnblogs.com/liwenzhou/p/8343243.html 一.Cookie Cookie是什么? 就是保存在客户端浏览器上的键值对. Cookie为什么存在? 因 ...