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

  1. 2 17
  2. 14 17

Sample Output

  1. 2,3 are closest, 7,11 are most distant.
  2. There are no adjacent primes.
  3.  
  4. 主要思想是偏移数组,区间素数打表。
  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<queue>
  5. typedef long long LL;
  6. #include<algorithm>
  7. using namespace std;
  8. #define N 1000010
  9. int notprime[N];
  10. int prime[N];
  11. int prime2[N];
  12. bool vis[N];
  13. bool val[N];
  14. int pn=0;
  15.  
  16. void getPrime()
  17. {
  18. memset(prime,0,sizeof(prime));
  19. for(int i=2;i<=N;i++)
  20. {
  21. if(!prime[i])prime[++prime[0]]=i;
  22. for(int j=1;j<=prime[0]&&prime[j]<=N/i;j++)
  23. {
  24. prime[prime[j]*i]=1;
  25. if(i%prime[j]==0)break;
  26. }
  27. }
  28. }
  29. void getPrime2(int L,int R)
  30. {
  31. memset(notprime,false,sizeof(notprime));
  32. if(L<2)L=2;
  33. for(int i=1;i<=prime[0]&&(long long)prime[i]*prime[i]<=R;i++)
  34. {
  35. int s=L/prime[i]+(L%prime[i]>0);
  36. if(s==1)s=2;
  37. for(int j=s;(long long)j*prime[i]<=R;j++)
  38. if((long long)j*prime[i]>=L)
  39. notprime[j*prime[i]-L]=true;
  40. }
  41. prime2[0]=0;
  42. for(int i=0;i<=R-L;i++)
  43. if(!notprime[i])
  44. prime2[++prime2[0]]=i+L;
  45. }
  46.  
  47. int main()
  48. {
  49. getPrime();
  50. LL m,t,h;
  51. int l,r;
  52. while(~scanf("%d%d",&l,&r))
  53. {
  54. int sh1=0,sh2=1000000,lo1=0,lo2=0;
  55. getPrime2(l,r);
  56. if(prime2[0]<2)
  57. {
  58. puts("There are no adjacent primes.");
  59. continue;
  60. }
  61. for(int i=1;i<prime2[0];i++)
  62. {
  63. if(sh2-sh1>prime2[i+1]-prime2[i])
  64. {
  65. sh1=prime2[i];
  66. sh2=prime2[i+1];
  67. }
  68. if(lo2-lo1<prime2[i+1]-prime2[i])
  69. {
  70. lo1=prime2[i];
  71. lo2=prime2[i+1];
  72. }
  73. }
  74. printf("%d,%d are closest, %d,%d are most distant.\n",sh1,sh2,lo1,lo2);
  75. }
  76. }

  

poj_2689_Prime Distance的更多相关文章

  1. [LeetCode] Total Hamming Distance 全部汉明距离

    The Hamming distance between two integers is the number of positions at which the corresponding bits ...

  2. [LeetCode] Hamming Distance 汉明距离

    The Hamming distance between two integers is the number of positions at which the corresponding bits ...

  3. [LeetCode] Rearrange String k Distance Apart 按距离为k隔离重排字符串

    Given a non-empty string str and an integer k, rearrange the string such that the same characters ar ...

  4. [LeetCode] Shortest Distance from All Buildings 建筑物的最短距离

    You want to build a house on an empty land which reaches all buildings in the shortest amount of dis ...

  5. [LeetCode] Shortest Word Distance III 最短单词距离之三

    This is a follow up of Shortest Word Distance. The only difference is now word1 could be the same as ...

  6. [LeetCode] Shortest Word Distance II 最短单词距离之二

    This is a follow up of Shortest Word Distance. The only difference is now you are given the list of ...

  7. [LeetCode] Shortest Word Distance 最短单词距离

    Given a list of words and two words word1 and word2, return the shortest distance between these two ...

  8. [LeetCode] One Edit Distance 一个编辑距离

    Given two strings S and T, determine if they are both one edit distance apart. 这道题是之前那道Edit Distance ...

  9. [LeetCode] Edit Distance 编辑距离

    Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...

随机推荐

  1. Node调试之node-inspect工具

    1.全局安装node-inspect模块: npm install -g node-inspect 2.通过谷歌浏览器打开:chrome://flags/#enable-devtools-experi ...

  2. git 基础教程

    git 提交 全部文件 git add .  git add xx命令可以将xx文件添加到暂存区,如果有很多改动可以通过 git add -A .来一次添加所有改变的文件.注意 -A 选项后面还有一个 ...

  3. eureka的一点细节

    第二部分粗略的过一遍,还是有些模糊,再来相对系统的看一下: ---------------------------------------------------------------------- ...

  4. 设置webstorm的file watch 监视scss文件

    参考:http://blog.founddrama.net/2013/04/watching-compass-files-in-webstorm/ 上面红色划线部分. 特别注意arguments: 像 ...

  5. javascript获取行间样式和非行间样式--兼容写法

    style:获取行间样式: currentStyle:获取计算后的样式,也叫当前样式.最终样式. 优点:可以获取元素的最终样式,包括浏览器的默认值,而不像style只能获取行间样式,所以更常用到.注意 ...

  6. fastjson的json字符串转List

     1.代码 gameListStr = "[{"gameId":"1","gameName":"哈哈"},{& ...

  7. c#-day03学习笔记

    循环语句 一共有三种 1: For循环 2: while 循环 3: do while 循环 //1             //2             //4 For循环  语法       f ...

  8. js数组Array方法

    1. indexOf indexOf()方法返回在该数组中第一个找到的元素位置,如果它不存在则返回-1. var fruits = ["Banana", "Orange& ...

  9. codevs原创抄袭题 5960 信使

    题目描述 Description •战争时期,前线有n个哨所,每个哨所可能会与其他若干个哨所之间有通信联系.信使负责在哨所之间传递信息,当然,这是要花费一定时间的(以天为单位).指挥部设在第一个哨所. ...

  10. python反爬之封IP

    # requests是第三方库,需要安装 pip install requests import requests # 在日常的爬虫中,封ip也是一个很常用的反爬虫手段,遇到这种情况,我们只需要在每次 ...