POJ 2689 - Prime Distance 任意区间内筛素数
来源:互联网 发布:cgwang插画网络 编辑:程序博客网 时间:2024/05/01 04:42
和1~n范围内筛素数差不多...只不过对于每个素数加了一个偏移量了..比如在5 10这个区间..筛掉2的倍数..那么从6开始剔除...由于题目所给的数据..最大的合数..其质因子不会大于10^5..所以先用传统的筛素数法把1~10^5内的素数找出..然后再来筛[L,U]这个区间的素数...
Program:
#include<iostream>#include<stdio.h>#include<string.h>#include<cmath>#include<queue>#include<stack>#include<set>#include<algorithm>#define ll long long #define oo 1000000007#define pi acos(-1.0)#define MAXN 1000005using namespace std;ll Prime[MAXN],ans[MAXN];int Pnum,anum;bool P[MAXN];void PreWork(){ int i,x; memset(P,true,sizeof(P)); for (Pnum=0,i=2;i<=100000;i++) if (P[i]) for (Prime[++Pnum]=i,x=i*2;x<=100000;x+=i) P[x]=false; }int main(){ PreWork(); for (ll L,U;~scanf("%I64d%I64d",&L,&U);) { ll n=U-L; memset(P,true,sizeof(P)); for (int i=1;i<=Pnum;i++) { ll h=Prime[i],x; if (h*h>U) break; x=(h-(L%h))%h; if (L+x==h) x+=h; for (;x<=n;x+=h) P[x]=false; } if (L==1) P[0]=false; anum=0; for (int i=0;i<=n;i++) if (P[i]) ans[++anum]=i+L; if (anum<2) printf("There are no adjacent primes.\n"); else { int i,a,b; a=b=1; for (i=1;i<anum;i++) { if (ans[i+1]-ans[i]<ans[a+1]-ans[a]) a=i; if (ans[i+1]-ans[i]>ans[b+1]-ans[b]) b=i; } printf("%I64d,%I64d are closest, %I64d,%I64d are most distant.\n",ans[a],ans[a+1],ans[b],ans[b+1]); } } return 0;}
- POJ 2689 - Prime Distance 任意区间内筛素数
- Poj 2689 Prime Distance【素数区间筛】
- poj 2689 Prime Distance(筛一个区间内的素数(或合数))
- POJ 2689-Prime Distance(区间素数)
- poj 2689 Prime Distance(大区间筛素数)
- poj 2689 Prime Distance 筛法/二次筛法/区间素数
- poj 2689 Prime Distance 筛法+区间筛素数
- poj 2689 Prime Distance(大数区间素数筛法)
- POJ-2689 Prime Distance(区间素数筛--经典题)
- POJ 2689 Prime Distance(区间素数筛)
- POJ 2689 Prime Distance 【大区间筛素数】
- POJ 2689 Prime Distance(区间相邻素数最大、小距离,区间素数筛)
- Prime Distance poj 2689 区间内的素数打表模板
- Poj 2689 Prime Distance ----大区间素数筛选
- POJ - 2689 Prime Distance(大区间素数筛选)
- [ACM] POJ 2689 Prime Distance (大区间素数筛选)
- POJ题目2689 Prime Distance(任何区间素数筛选)
- poj 2689 Prime Distance 二次筛素数
- 在Java中直接调用js代码
- GREENPLUM介绍之数据库管理(四)-创建数据库和用户管理
- 为啥REST如此重要?
- 讲讲简单的接地方式
- 一个通过JSONP跨域调用WCF REST服务的例子(以jQuery为例)
- POJ 2689 - Prime Distance 任意区间内筛素数
- ORACLE 定时任务 JOB_QUEUE_PROCESSES 的设置
- 网络编程基础
- 创建文件并进行读写
- 如何搭建Flash P2P网络直播点播平台
- JAVA的StringBuffer类
- hdu 2028 Lowest Common Multiple Plus
- 按钮位置编辑
- 非对称双核MCU基础知识及核间通信