poj_2689_Prime Distance
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.
- 主要思想是偏移数组,区间素数打表。
- #include<iostream>
- #include<cstdio>
- #include<cstring>
- #include<queue>
- typedef long long LL;
- #include<algorithm>
- using namespace std;
- #define N 1000010
- int notprime[N];
- int prime[N];
- int prime2[N];
- bool vis[N];
- bool val[N];
- int pn=0;
- void getPrime()
- {
- memset(prime,0,sizeof(prime));
- for(int i=2;i<=N;i++)
- {
- if(!prime[i])prime[++prime[0]]=i;
- for(int j=1;j<=prime[0]&&prime[j]<=N/i;j++)
- {
- prime[prime[j]*i]=1;
- if(i%prime[j]==0)break;
- }
- }
- }
- void getPrime2(int L,int R)
- {
- memset(notprime,false,sizeof(notprime));
- if(L<2)L=2;
- for(int i=1;i<=prime[0]&&(long long)prime[i]*prime[i]<=R;i++)
- {
- int s=L/prime[i]+(L%prime[i]>0);
- if(s==1)s=2;
- for(int j=s;(long long)j*prime[i]<=R;j++)
- if((long long)j*prime[i]>=L)
- notprime[j*prime[i]-L]=true;
- }
- prime2[0]=0;
- for(int i=0;i<=R-L;i++)
- if(!notprime[i])
- prime2[++prime2[0]]=i+L;
- }
- int main()
- {
- getPrime();
- LL m,t,h;
- int l,r;
- while(~scanf("%d%d",&l,&r))
- {
- int sh1=0,sh2=1000000,lo1=0,lo2=0;
- getPrime2(l,r);
- if(prime2[0]<2)
- {
- puts("There are no adjacent primes.");
- continue;
- }
- for(int i=1;i<prime2[0];i++)
- {
- if(sh2-sh1>prime2[i+1]-prime2[i])
- {
- sh1=prime2[i];
- sh2=prime2[i+1];
- }
- if(lo2-lo1<prime2[i+1]-prime2[i])
- {
- lo1=prime2[i];
- lo2=prime2[i+1];
- }
- }
- printf("%d,%d are closest, %d,%d are most distant.\n",sh1,sh2,lo1,lo2);
- }
- }
poj_2689_Prime Distance的更多相关文章
- [LeetCode] Total Hamming Distance 全部汉明距离
The Hamming distance between two integers is the number of positions at which the corresponding bits ...
- [LeetCode] Hamming Distance 汉明距离
The Hamming distance between two integers is the number of positions at which the corresponding bits ...
- [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 ...
- [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 ...
- [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 ...
- [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 ...
- [LeetCode] Shortest Word Distance 最短单词距离
Given a list of words and two words word1 and word2, return the shortest distance between these two ...
- [LeetCode] One Edit Distance 一个编辑距离
Given two strings S and T, determine if they are both one edit distance apart. 这道题是之前那道Edit Distance ...
- [LeetCode] Edit Distance 编辑距离
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...
随机推荐
- Node调试之node-inspect工具
1.全局安装node-inspect模块: npm install -g node-inspect 2.通过谷歌浏览器打开:chrome://flags/#enable-devtools-experi ...
- git 基础教程
git 提交 全部文件 git add . git add xx命令可以将xx文件添加到暂存区,如果有很多改动可以通过 git add -A .来一次添加所有改变的文件.注意 -A 选项后面还有一个 ...
- eureka的一点细节
第二部分粗略的过一遍,还是有些模糊,再来相对系统的看一下: ---------------------------------------------------------------------- ...
- 设置webstorm的file watch 监视scss文件
参考:http://blog.founddrama.net/2013/04/watching-compass-files-in-webstorm/ 上面红色划线部分. 特别注意arguments: 像 ...
- javascript获取行间样式和非行间样式--兼容写法
style:获取行间样式: currentStyle:获取计算后的样式,也叫当前样式.最终样式. 优点:可以获取元素的最终样式,包括浏览器的默认值,而不像style只能获取行间样式,所以更常用到.注意 ...
- fastjson的json字符串转List
1.代码 gameListStr = "[{"gameId":"1","gameName":"哈哈"},{& ...
- c#-day03学习笔记
循环语句 一共有三种 1: For循环 2: while 循环 3: do while 循环 //1 //2 //4 For循环 语法 f ...
- js数组Array方法
1. indexOf indexOf()方法返回在该数组中第一个找到的元素位置,如果它不存在则返回-1. var fruits = ["Banana", "Orange& ...
- codevs原创抄袭题 5960 信使
题目描述 Description •战争时期,前线有n个哨所,每个哨所可能会与其他若干个哨所之间有通信联系.信使负责在哨所之间传递信息,当然,这是要花费一定时间的(以天为单位).指挥部设在第一个哨所. ...
- python反爬之封IP
# requests是第三方库,需要安装 pip install requests import requests # 在日常的爬虫中,封ip也是一个很常用的反爬虫手段,遇到这种情况,我们只需要在每次 ...