题目地址: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 (素数+两次筛选)的更多相关文章

  1. poj 2689 Prime Distance(大区间素数)

    题目链接:poj 2689 Prime Distance 题意: 给你一个很大的区间(区间差不超过100w),让你找出这个区间的相邻最大和最小的两对素数 题解: 正向去找这个区间的素数会超时,我们考虑 ...

  2. poj 2689 Prime Distance (素数二次筛法)

    2689 -- Prime Distance 没怎么研究过数论,还是今天才知道有素数二次筛法这样的东西. 题意是,要求求出给定区间内相邻两个素数的最大和最小差. 二次筛法的意思其实就是先将1~sqrt ...

  3. [ACM] POJ 2689 Prime Distance (筛选范围大素数)

    Prime Distance Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12811   Accepted: 3420 D ...

  4. poj 2689 Prime Distance(区间筛选素数)

    Prime Distance Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9944   Accepted: 2677 De ...

  5. 数论 - 素数的运用 --- poj 2689 : Prime Distance

    Prime Distance Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12512   Accepted: 3340 D ...

  6. POJ 2689 Prime Distance(素数筛选)

    题目链接:http://poj.org/problem?id=2689 题意:给出一个区间[L, R],找出区间内相连的,距离最近和距离最远的两个素数对.其中(1<=L<R<=2,1 ...

  7. POJ 2689 Prime Distance (素数筛选法,大区间筛选)

    题意:给出一个区间[L,U],找出区间里相邻的距离最近的两个素数和距离最远的两个素数. 用素数筛选法.所有小于U的数,如果是合数,必定是某个因子(2到sqrt(U)间的素数)的倍数.由于sqrt(U) ...

  8. poj 2689 Prime Distance(大区间筛素数)

    http://poj.org/problem?id=2689 题意:给出一个大区间[L,U],分别求出该区间内连续的相差最小和相差最大的素数对. 由于L<U<=2147483647,直接筛 ...

  9. 题解报告:poj 2689 Prime Distance(区间素数筛)

    Description The branch of mathematics called number theory is about properties of numbers. One of th ...

随机推荐

  1. Dreamweaver 8

    Dreamweaver 8 附注册码:WPD800-56030-83832-97910

  2. 【环境】VS2013和MATLAB相互调用混合编程

    Visual Studio和MATLAB混合编程,有两种方法: 1 MATLAB调用C程序: 2 VS调用MATLAB(目前见到的都是VS,其他编译器如codeblocks,或不提供这项功能): 前一 ...

  3. shell编程之数学运算

    shell数学运算支持整数运算的四种方法 1.let命令 no1=4; no2=5; let result=no1+no2 2.[]操作符 result=$[ no1 + no2] 3.(())操作符 ...

  4. 【BZOJ】【1011】【HNOI2008】遥远的行星

    神奇的思路题QAQ 玛雅看到这题我就醉了,什么玩意……5%的误差?果断膜拜@ydc神犇的题解: 就是因为不清楚如何应用那个答案误差不超过5%啦. 从没见过这么诡异的题一下就懵了,问到了方法之后都还半信 ...

  5. phonegap/cordova常用命令

    创建项目 cordova create foldername com.wps.test projectName cd foldername 基本设备信息 设备 API: cordova plugin ...

  6. Vim 中使用cscope

    使用cscope碰到的问题 1. E568: duplicate cscope database not added 根据提示,cscope数据库重复添加了,我使用的是vim7.2版本,而这个版本在已 ...

  7. slot的含义

    1) slot就是槽的意思,是一个资源单位,只有给task分配了一个slot之后,这个task才可以运行.slot分两种,map slot沪蓉reduce slot.另外,slot是一个逻辑概念,一个 ...

  8. D&F学数据结构系列——前驱和后继

    前驱和后继 本文所述为二叉排序树的前驱和后继,如果想了解二叉排序树的概念,可以参考我的博文http://www.cnblogs.com/sage-blog/p/3864640.html 给定一个二叉查 ...

  9. jvm 之 国际酒店 8 月 19 一次full GC 导致的事故

    事故经过: 1  15:18收到短信报警:国际酒店调用OMS queryGorderOrderList方法失败:成单接口调用OMS获取token失败. 2  查看checkList发现15:18开始发 ...

  10. Android——横屏和竖屏的切换,以及明文密码的显示

    查看API文档: android.content.pm.ActivityInfo    在手机的使用中,我们要根据不同的需求来改变屏幕的显示方向,一般在浏览信息时是竖屏,在玩游戏的时候就要切换到横屏. ...