POJ 2689 Prime Distance (素数+两次筛选)
题目地址:http://poj.org/problem?id=2689
题意:给你一个不超过1000000的区间L-R,要你求出区间内相邻素数差的最大最小值,输出相邻素数。
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <list>
#include <deque>
#include <queue>
#include <iterator>
#include <stack>
#include <map>
#include <set>
#include <algorithm>
#include <cctype>
using namespace std; typedef long long LL;
const int N=1<<16;
const int M=1000005;
const int mod=1000007;
const int INF=0x3f3f3f3f;
const double PI=acos(-1.0); int pri[N],k;
void xh_phi()
{
int i,j;
memset(pri,0,sizeof(pri));
k=0;
for(i=2;i<=N;i++)
{
if(!pri[i])
{
pri[++k]=i;
for(j=i;j<=N;j+=i)
pri[j]=1;
}
}
} int prime[M],t;
bool nopri[M];
void getprime(int L,int R)
{
int i,j;
memset(nopri,false,sizeof(nopri));
if(L<2)
L=2;
for(i=1;i<=k&&(LL)pri[i]*pri[i]<=R;i++)
{
int s=L/pri[i]+(L%pri[i]>0);
if(s==1) s=2;
for(j=s;(LL)j*pri[i]<=R;j++)
if((LL)j*pri[i]>=L)
nopri[j*pri[i]-L]=true;
}
prime[0]=0;
t=0;
for(i=0;i<=R-L;i++)
if(!nopri[i])
{
prime[++t]=i+L;
}
} int main()
{
int n,m,i,j;
xh_phi();
while(~scanf("%d%d",&n,&m))
{
getprime(n,m);
int Mi=INF,Ma=0;
int x1,x2,y1,y2,f=0;
if(t<2)
{
printf("There are no adjacent primes.\n");
continue;
}
for(i=1;i<t;i++)
{
int p=prime[i+1]-prime[i];
if(p>Ma)
{
Ma=p;x2=prime[i];y2=prime[i+1];
}
if(p<Mi)
{
Mi=p;x1=prime[i];y1=prime[i+1];
}
}
printf("%d,%d are closest, %d,%d are most distant.\n",x1,y1,x2,y2); }
return 0;
}
POJ 2689 Prime Distance (素数+两次筛选)的更多相关文章
- poj 2689 Prime Distance(大区间素数)
题目链接:poj 2689 Prime Distance 题意: 给你一个很大的区间(区间差不超过100w),让你找出这个区间的相邻最大和最小的两对素数 题解: 正向去找这个区间的素数会超时,我们考虑 ...
- poj 2689 Prime Distance (素数二次筛法)
2689 -- Prime Distance 没怎么研究过数论,还是今天才知道有素数二次筛法这样的东西. 题意是,要求求出给定区间内相邻两个素数的最大和最小差. 二次筛法的意思其实就是先将1~sqrt ...
- [ACM] POJ 2689 Prime Distance (筛选范围大素数)
Prime Distance Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12811 Accepted: 3420 D ...
- poj 2689 Prime Distance(区间筛选素数)
Prime Distance Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9944 Accepted: 2677 De ...
- 数论 - 素数的运用 --- poj 2689 : Prime Distance
Prime Distance Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12512 Accepted: 3340 D ...
- POJ 2689 Prime Distance(素数筛选)
题目链接:http://poj.org/problem?id=2689 题意:给出一个区间[L, R],找出区间内相连的,距离最近和距离最远的两个素数对.其中(1<=L<R<=2,1 ...
- POJ 2689 Prime Distance (素数筛选法,大区间筛选)
题意:给出一个区间[L,U],找出区间里相邻的距离最近的两个素数和距离最远的两个素数. 用素数筛选法.所有小于U的数,如果是合数,必定是某个因子(2到sqrt(U)间的素数)的倍数.由于sqrt(U) ...
- poj 2689 Prime Distance(大区间筛素数)
http://poj.org/problem?id=2689 题意:给出一个大区间[L,U],分别求出该区间内连续的相差最小和相差最大的素数对. 由于L<U<=2147483647,直接筛 ...
- 题解报告:poj 2689 Prime Distance(区间素数筛)
Description The branch of mathematics called number theory is about properties of numbers. One of th ...
随机推荐
- android开发 解决启动页空白或黑屏问题
遇到的情况: app启动时进入启动页时出现白屏页,然后大概一秒之后就出现了背景图片. 原因:app启动时加载的是windows背景,之后再加载布局文件的,所以开始的黑屏/白屏就是windows的背景颜 ...
- WPF中的Style(风格,样式)(转)
在WPF中我们可以使用Style来设置控件的某些属性值,并使该设置影响到指定范围内的所有该类控件或影响指定的某一控件,比如说我们想将窗口中的所有按钮都保持某一种风格,那么我们可以设置一个Style,而 ...
- Ubuntu 16.04 下卸载 lnmp/lamp 方法
1.卸载 apache2 sudo apt-get --purge remove apache2* sudo apt-get autoremove apache2 (--purge 是完全删除并且不保 ...
- 《JavaScript高级程序设计》第3章 基本概念
3.4 数据类型 3.4.1 typeof操作符 var message = 'some string'; console.log(typeof message); // 'string' conso ...
- shell编程之重定向
标准输入,输出和错误---------------------------------文件文件 描述符---------------------------------输 ...
- 了解javascript中的事件(二)
本文目录如下: 零.寒暄 一.事件的分类 二.事件代理 2.1 问题引出 2.2 什么是事件代理 2.3 完整示例 二.事件代理 三.事件代理思想的用处 四.总结 零.寒暄 这篇博客本该出现在两个月以 ...
- 查看windows系统热键占用情况
有时候我们经常用一些软件中的快捷键,但是会发现快捷键设置的很正确,但是就是不起作用.这就是因为这些快捷键被系统或者其他软件占用了. 那么这时我们怎么知道是哪个软件占用了呢?这确实是个纠结的问题,还好大 ...
- java web项目,java类中获得WEB-INF路径
private static String getWebInfPath() { URL url = 当前类.class.getProtectionDomain().getCodeSource().ge ...
- js java正则表达式替换手机号4-7位为星*号
需求: 一个手机号13152461111,由于安全性,需要替换4-7位字符串为星号,为131****1111,那么有2中玩法,一种是前端隐藏,一种是后台隐藏. 1. 前台隐藏 <!DOCTYPE ...
- maven mirror repository
简单点来说,repository就是个仓库.maven里有两种仓库,本地仓库和远程仓库.远程仓库相当于公共的仓库,大家都能看到.本地仓库是你本地的一个山寨版,只有你看的到,主要起缓存作用.当你向仓库请 ...