Prime Distance POJ - 2689 (数学 素数)
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. 题意:找到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 (数学 素数)的更多相关文章
- Prime Distance POJ - 2689 线性筛
一个数 $n$ 必有一个不超过 $\sqrt n$ 的质因子. 打表处理出 $1$ 到 $\sqrt n$ 的质因子后去筛掉属于 $L$ 到 $R$ 区间的素数即可. Code: #include&l ...
- [ACM] POJ 2689 Prime Distance (筛选范围大素数)
Prime Distance Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12811 Accepted: 3420 D ...
- POJ2689 Prime Distance(数论:素数筛选模板)
题目链接:传送门 题目: Prime Distance Time Limit: 1000MS Memory Limit: 65536K Total Submissions: Accepted: Des ...
- poj 2689 Prime Distance(大区间筛素数)
http://poj.org/problem?id=2689 题意:给出一个大区间[L,U],分别求出该区间内连续的相差最小和相差最大的素数对. 由于L<U<=2147483647,直接筛 ...
- POJ2689:Prime Distance(大数区间素数筛)
The branch of mathematics called number theory is about properties of numbers. One of the areas that ...
- poj 2689 区间素数筛
The branch of mathematics called number theory is about properties of numbers. One of the areas that ...
- poj 2689 巧妙地运用素数筛选
称号: 给出一个区间[L,R]求在该区间内的素数最短,最长距离. (R < 2 * 10^9 , R - L <= 10 ^ 6) 由数论知识可得一个数的因子可在开根号内得到. 所以,我们 ...
- poj 2689 (素数二次筛选)
Sample Input 2 17 14 17 Sample Output 2,3 are closest, 7,11 are most distant. There are no adjacent ...
- [POJ268] Prime Distance(素数筛)
/* * 二次筛素数 * POJ268----Prime Distance(数论,素数筛) */ #include<cstdio> #include<vector> using ...
随机推荐
- 主席树[可持久化线段树](hdu 2665 Kth number、SP 10628 Count on a tree、ZOJ 2112 Dynamic Rankings、codeforces 813E Army Creation、codeforces960F:Pathwalks )
在今天三黑(恶意评分刷上去的那种)两紫的智推中,突然出现了P3834 [模板]可持久化线段树 1(主席树)就突然有了不详的预感2333 果然...然后我gg了!被大佬虐了! hdu 2665 Kth ...
- 用IntelliJ IDEA 开发Spring+SpringMVC+Mybatis框架 分步搭建一:建立MAVEN Web项目
一:创建maven web项目er
- iOS开发者知识普及,Swift 挑战 Objective-C,谁会笑到最后?
前言: 目前全球共有超过 7 亿台 iPhone 处于活跃状态,全球约有2000万名 iOS 开发者,这造就了 iOS 作为全球第二大移动设备平台的状态. 虽然安卓系统的全球市场占有率超过 iOS 系 ...
- <二>企业级开源仓库nexus3实战应用–使用nexus3配置docker私有仓库
1,安装nexus3. 这个地方略了,安装部署可以参考:nexus3安装配置. 2,配置走起. 1,创建blob存储. 登陆之后,先创建一个用于存储镜像的空间. 定义一个name,下边的内容会自动补全 ...
- 关于百度地图(离线)使用过程报“Cannot read property 'jb' of undefined ”错误的解决办法
使用百度地图(离线)API时,地图无法显示,f12查看报错: BaiduApi_2.0.js:1056 Uncaught TypeError: Cannot read property 'jb' of ...
- ACM-ICPC 2018 徐州赛区网络预赛 G Trace(思维+set)
https://nanti.jisuanke.com/t/31459 题意 n个矩阵,不存在包含,矩阵左下角都在(0,0),给右上角坐标,后来的矩阵会覆盖前面的矩阵,求矩阵周长. 分析 set按照x或 ...
- HDU 5983(模拟魔方 模拟)
题意是说给定一个 2*2 魔方的各个面的情况,问是否能转动不超过一次使得魔方复原. 思路是先在输入的时候统计一下已完成的面数,要想以最多一次的转动使得魔方复原,那么已完成的面数只能是 2 面或者 6 ...
- Richard Sabey于2004年给出了由123456789各出现一次的e的估计
- h3c mstp的举例
h3c交换机的图如下: 分别对于SWA,SWB,SWC,SWD,SWE 配置如下: SWA: vlan 10 vlan 20 vlan 30 region-name h3c instance 0 vl ...
- 部署自己的服务器ubuntu
一直都是在公司的服务器上工作,想搞点自己的idea比较不方便,所以近期租了要给自己的阿里云服务器. 以下为必要的软件的安装流程: jdk+jre: 1.去官网下载 jdk-linux版本: 2.解压压 ...