题目地址: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. android开发 wifi开发工具类

    import java.lang.reflect.Field; import java.lang.reflect.Method; import java.util.Iterator; import j ...

  2. JS 学习笔记--12---面向对象

    练习中使用的浏览器为IE10,如果各位朋友有不同意见或者本文有什么错误地方,望指正 ECMASCript有两种开发模式:函数式(面向过程)和面向对象.面向对象有一个很明显的标志,那就是类,我们可以通过 ...

  3. bzoj 3232 01分数规划+最大权封闭子图判定

    我们的目标是使v/c最小化,所以构造函数g(x)=v-x*c,那么 二分一个X,判断当时的v-x*c的值是多少,然后根据g(x)函数的 单调递减性来二分,判断,直到g(x)=0的时候当前的X就是答案. ...

  4. Codeforces Round #284 (Div. 2)

    题目链接:http://codeforces.com/contest/499 A. Watching a movie You have decided to watch the best moment ...

  5. UIResponder

    原网址:http://www.cnblogs.com/kuku/archive/2011/11/12/2246389.html 在 iOS 中,一个 UIResponder 对象表示一个可以接收触摸屏 ...

  6. UML构建模块(转载)

    UML描述的实时系统,这是非常重要的一个概念模型,然后进行逐渐. UML的概念模型可以通过学习掌握以下三大要素: UML构建模块 规则连接构建模块 UML的公共机制 本章介绍了所有的UML构建块. U ...

  7. imageNamed 与 imageWithContentsOfFile的区别

    如题,是不是大家为了方便都这样加载图片啊 myImage = [UIImage imageNamed:@"icon.png"];那么小心了这种方法在一些图片很少,或者图片很小的程序 ...

  8. SOA之(2)——SOA架构基础概念与设计框架

    SOA的设计框架 设计框架与架构相关的概念紧密相连,原则.模式和架构始终是与设计共舞的. SOA服务设计的原则中记录了一个基础的设计框架: 设计特性(Design Characteristic)——由 ...

  9. HDU 4022 Bombing(stl,map,multiset,iterater遍历)

    题目 参考了     1     2 #define _CRT_SECURE_NO_WARNINGS //用的是STL中的map 和 multiset 来做的,代码写起来比较简洁,也比较好容易理解. ...

  10. WPF、Windows Forms和Silverlight间的联系和区别(转)

    WPF.Windows Forms和Silverlight间的联系和区别http://blog.csdn.net/bitfan/article/details/6128391 .NET Windows ...