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 ...
随机推荐
- DBCP数据连接池
package com.itheima.utils; import java.io.InputStream; import java.sql.Connection; import java.sql.R ...
- 牛客网Java刷题知识点之File对象常用功能:获取文件名称、获取文件路径、获取文件大小、获取文件修改时间、创建与删除、判断、重命名、查看系统根目录、容量获取、获取某个目录下内容、过滤器
不多说,直接上干货! 获取文件名称.获取文件路径.获取文件大小.获取文件修改时间 FileMethodDemo.java package zhouls.bigdata.DataFeatureSelec ...
- IE浏览器兼容性问题解决方案
一.CSS常见问题 1.H5标签兼容性 解决方案:<script src="http://cdn.bootcss.com/html5shiv/3.7.3/html5shiv.min.j ...
- 异步对象(XMLHttpRequest)的帮助脚本
异步对象五部曲 这是post请求的. //1.00创建异步对象 var xhr = new XMLHttpRequest(); //2.0 xhr.open("post", url ...
- Sublime Text Emmet插件 : 生成html,css 快捷键
(输入下面简写,按Tab键可触发效果,或者 ctrl + e) html缩写 输入 !后 按下 ctrl + e : 结果 <!DOCTYPE html><html lang=&qu ...
- http-equiv和name的区别
meta是用来在HTML文档中模拟HTTP协议的响应头报文.meta 标签用于网页的<head>与</head>中,meta 标签的用处很多.meta 的属性有两种:name和 ...
- <Android 基础(十四)> selector
介绍 A StateListDrawable is a drawable object defined in XML that uses a several different images to r ...
- 关于form的action路径填写
一:可以是相对路径: 1.action="<%=request.getContextPath() %>/html/index.html" <%=request. ...
- 老笔记本装xubuntu+win7
https://www.freezhongzi.info/?p=167 1 install xubuntu 分区如下 sda1 20g ext4 / sda2 80g ext4 /data sda3 ...
- android三大组件之Intent
Android 应用程序中有三大核心组件: Activity, Service, Broadcast Receiver 都是通过被称之为意图的消息运行. Intent messaging is a f ...