题目地址: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. SD 销售订单的BADI增强SD_BADI_PRICE_CHECK

    METHOD if_ex_badi_sd_sales~save_document.* CALL FUNCTION 'POPUP_TO_DISPLAY_TEXT'* EXPORTING* titel = ...

  2. SqlServer 临时表、表变量、函数 替代游标

    http://www.cnblogs.com/chongzi/archive/2011/01/19/1939106.html 临时表 存放在tempdb中 --存储过程中将多表连接结果写入到临时表中, ...

  3. poj 1986 Distance Queries LCA

    题目链接:http://poj.org/problem?id=1986 Farmer John's cows refused to run in his marathon since he chose ...

  4. 【BZOJ】【1912】【APIO2010】patrol巡逻

    树形DP 说是树形DP,其实就是求树的最长链嘛…… K=1的时候明显是将树的最长链的两端连起来最优. 但是K=2的时候怎么搞? 考虑第一次找完树的最长链以后的影响:第一次找过的边如果第二次再走,对答案 ...

  5. ID3d11asynchronous

    http://msdn.microsoft.com/en-us/library/windows/desktop/ff476428(v=vs.85).aspx 这东西 该怎么用 ! 照这位兄弟的做就可以 ...

  6. Linux软件安装方法小结(附:rpm详解)(转载)

    在使用Linux系统的过程中,软件包的安装是避免不了的,在Linux下,软件安装程序的种类很多,安装方法也各式各样,(舒适性自然比不上windows :-))不过我们常见的软件包有两种: 1)含有软件 ...

  7. PostgreSQL数据库中获取表主键名称

    PostgreSQL数据库中获取表主键名称 一.如下表示,要获取teacher表的主键信息: select pg_constraint.conname as pk_name,pg_attribute. ...

  8. Javascript在页面加载时的执行顺序【转】

    一.在HTML中嵌入Javasript的方法 直接在Javascript代码放在标记对<script>和</script>之间 由<script />标记的src属 ...

  9. SOA之(5)——REST的SOA(SOA with REST)概念

    REST的SOA(SOA with REST)概念 发展 1992年网站(Web Sites)是在Web浏览器和Web服务器直接通过HTTP传输HTML. 2000年WS-* (Web Service ...

  10. __dict__和__slots__

    __dict__: __slots__: