The branch of mathematics called number theory is about properties of numbers. One of the areas that has captured the interest of number theoreticians for thousands of years is the question of primality. A prime number is a number that is has no proper factors (it is only evenly divisible by 1 and itself). The first prime numbers are 2,3,5,7 but they quickly become less frequent. One of the interesting questions is how dense they are in various ranges. Adjacent primes are two numbers that are both primes, but there are no other prime numbers between the adjacent primes. For example, 2,3 are the only adjacent primes that are also adjacent numbers.
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

Each line of input will contain two positive integers, L and U, with L < U. The difference between L and U will not exceed 1,000,000.

Output

For each L and U, the output will either be the statement that there are no adjacent primes (because there are less than two primes between the two given numbers) or a line giving the two pairs of adjacent primes.

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 (数学 素数)的更多相关文章

  1. Prime Distance POJ - 2689 线性筛

    一个数 $n$ 必有一个不超过 $\sqrt n$ 的质因子. 打表处理出 $1$ 到 $\sqrt n$ 的质因子后去筛掉属于 $L$ 到 $R$ 区间的素数即可. Code: #include&l ...

  2. [ACM] POJ 2689 Prime Distance (筛选范围大素数)

    Prime Distance Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12811   Accepted: 3420 D ...

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

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

  4. poj 2689 Prime Distance(大区间筛素数)

    http://poj.org/problem?id=2689 题意:给出一个大区间[L,U],分别求出该区间内连续的相差最小和相差最大的素数对. 由于L<U<=2147483647,直接筛 ...

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

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

  6. poj 2689 区间素数筛

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

  7. poj 2689 巧妙地运用素数筛选

    称号: 给出一个区间[L,R]求在该区间内的素数最短,最长距离. (R < 2 * 10^9 , R - L <= 10 ^ 6) 由数论知识可得一个数的因子可在开根号内得到. 所以,我们 ...

  8. poj 2689 (素数二次筛选)

    Sample Input 2 17 14 17 Sample Output 2,3 are closest, 7,11 are most distant. There are no adjacent ...

  9. [POJ268] Prime Distance(素数筛)

    /* * 二次筛素数 * POJ268----Prime Distance(数论,素数筛) */ #include<cstdio> #include<vector> using ...

随机推荐

  1. 【BZOJ5492】[HNOI2019]校园旅行(bfs)

    [HNOI2019]校园旅行(bfs) 题面 洛谷 题解 首先考虑暴力做法怎么做. 把所有可行的二元组全部丢进队列里,每次两个点分别向两侧拓展一个同色点,然后更新可行的情况. 这样子的复杂度是\(O( ...

  2. secureCRT自动断开的解决方法

    转: secureCRT自动断开的解决方法 secureCRT自动断开的解决方法 在secureCRT上登录时,一段时间不用的话会自动断开,必须重新连接,有点麻烦. 有时候服务器端的 /etc/pro ...

  3. SP687 REPEATS - Repeats

    给定字符串,求重复次数最多的连续重复子串. 题目很简单,被细节坑惨了... 前置的一个推论:请看这里. #include <bits/stdc++.h> using namespace s ...

  4. C++(1):error: invalid conversion from ‘void (*)()’ to ‘void (*)(int)

    void signaldemo_test(void) { struct itimerval tv, otv; signal(SIGALRM, sigFunc); //how long to run t ...

  5. Docker: 基础介绍 [一]

    一.Docker介绍 Docker是Docker.lnc公司开源的一个基于LXC技术之上构建的Container容器引擎,源代码托管在Github上,基于Go语言并遵从Apache2.0协议开源 Do ...

  6. 深入jar包:从jar包中读取资源文件getResourceAsStream

    一.背景 我们常常在代码中读取一些资源文件(比如图片,音乐,文本等等). 在单独运行的时候这些简单的处理当然不会有问题.但是,如果我们把代码打成一个jar包以后,即使将资源文件一并打包,这些东西也找不 ...

  7. 第八节:常见安全隐患和传统的基于Session和Token的安全校验

    一. 常见的安全隐患  1. SQL注入 常见的案例: String query = "SELECT * FROM T_User WHERE userID='" + Request ...

  8. JGUI源码:Accordion鼠标中键滚动和手机端滑动实现(2)

    本文是抽屉组件在PC端滚动鼠标中键.手机端滑动时,滚动数据列表实现方法,没有使用iscroll等第三方插件,支持火狐,谷歌,IE8+等浏览器. 演示在:www.jgui.com Github地址:ht ...

  9. 083_Remove Duplicates from Sorted List

    class ListNode: def __init__(self,x): self.val=x self.next=None ####注意这道题并不是把重复元素全部去掉而是保留一个#### #### ...

  10. artDialog记录

    //在子页面加按钮的方式 var api = frameElement.api, W = api.opener; api.button({ id: 'valueOk', name: '确定', cal ...