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 ...
随机推荐
- 020-pom.xml配置文件模板
1 Maven 整合SSH框架之pom.xml 1 版本一 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns: ...
- 实例化php类的时候如何传参
当我们实例化一个php类的时候,要怎么传递参数呢?这取决于该类的构造方法. 例: person.class.php <?php class person{ var $name; var $col ...
- http method and status code
http method HEAD: 只返回相应的header POST: 一般用于提交表单 PUT: 向Web服务器上传文件 GET: 查 DELET: 删除 status code 1xx与2xx: ...
- jq案例中遇到的知识点总结(会飞的小鸟和三级联动)
1.会飞的小鸟 ,按键盘的上下左右键,小鸟会上下左右的飞 知识点:1.keyCode 键盘按键对应的数字 比如 左上右下键 对应 37 38 39 40: 2.小鸟的位置:var bBird=$(&q ...
- Fibonacci(斐波那契数列)的第N位数
无穷数列1,1,2,3,5,8,13,21,34,55...称为Fibonacci数列,它可以递归地定义为F(n)=1 ...........(n=1或n=2)F(n)=F(n-1)+F(n-2).. ...
- 在UITableView中识别左右滑动,实现上下翻页的功能
目前有三种方案: 1. UIScrollView + UITableView. 实现方法,在UIScrollView中,加入UITableView即可 设置UIScrollView的代理和方法 - ( ...
- 自写Jq动画载入插件
在写网站的时候,有一些dom第一次进入屏幕时需要加一个动画进入效果,如下图 于是,自己就研究下,要是实现gif图中左图效果大致原理就是首先将dom放在他的左侧,并将他的透明度(opacity)设置为0 ...
- 在 CentOS 上安装 vsftp 服务
在 CentOS 上安装 vsftp 服务 1.查看当前 CentOS 服务器是否已安装了 vsftpd 服务: rpm -q vsftpd 如果打印如下类似的信息则表明已安装 vsftpd 服务: ...
- C++ Knowledge series STL & Const
Thank to the pepole who devote theirself to the common libs. STL(http://www.cplusplus.com/reference/ ...
- 修改Android系统关机动画
文件路径:frameworks\base\services\core\java\com\android\server\power\ShutdownThread.java 在beginShutdownS ...