称号:

给出一个区间[L,R]求在该区间内的素数最短,最长距离。 (R < 2 * 10^9 , R - L <= 10 ^ 6)

由数论知识可得一个数的因子可在开根号内得到。

所以,我们能够打出5*10^4内得素数。然后,在用一次筛法把在[L。R]内得合数找到,则剩下的就是素数了。这里要用到离散化。把一个数 x - L 保存在数组里。由于,直接保存肯定不行。可是我们发现区间特点较小。所以。能够想到离散化。

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std; typedef long long LL;
const int MAXN = 50000;
int primes[MAXN];
bool vst[MAXN];
int notPrimes[1000010];
int pos[1000010];
int top,pcnt; void init(){
top = 0;
memset(vst,0,sizeof(vst));
vst[0] = vst[1] = 1;
for(int i = 2;i < MAXN;++i)if(!vst[i]){
primes[top++] = i;
for(int j = i + i;j < MAXN;j += i) vst[j] = 1;
}
//printf("top: %d\n",top);
} void solve(int L,int R){
memset(notPrimes,0,sizeof(notPrimes)); if(L == 1) L = 2; /// 防止筛掉全部该区间的素数本身!!!!!
for(int i = 0;i < top&&(LL)primes[i]*primes[i] <= R;++i){ //筛选因子
int s = L / primes[i] + (L % primes[i] > 0); //当前素数的最小倍数达到L s = (s == 1 ? 2 : s); /// 防止筛掉全部该区间的素数本身!!!!! for(int j = s;(LL)j*primes[i] <= R;++j){
if((LL)j*primes[i] >= L) //合数
notPrimes[j*primes[i] - L] = 1; // 相当与离散化
}
} pcnt = 0;
for(int i = 0;i <= R - L;++i){
if(!notPrimes[i]){
pos[pcnt++] = i + L;
//printf("i -- > %d\n",i + L);
}
} if(pcnt < 2){
puts("There are no adjacent primes.");
} else {
int minl,minr,maxl,maxr,minv = 999999,maxv = -1;
for(int i = 1;i < pcnt;++i){
if(pos[i] - pos[i-1] > maxv){
maxv = pos[i] - pos[i-1];
maxl = pos[i-1];
maxr = pos[i];
}
if(pos[i] - pos[i-1] < minv){
minv = pos[i] - pos[i-1];
minl = pos[i-1];
minr = pos[i];
}
}
printf("%d,%d are closest, %d,%d are most distant.\n",minl,minr,maxl,maxr);
}
}
int main()
{
init();
int L,R;
while(~scanf("%d%d",&L,&R)){
solve(L,R);
}
return 0;
}

版权声明:本文博客原创文章,博客,未经同意,不得转载。

poj 2689 巧妙地运用素数筛选的更多相关文章

  1. poj 2689 (素数二次筛选)

    Sample Input 2 17 14 17 Sample Output 2,3 are closest, 7,11 are most distant. There are no adjacent ...

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

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

  3. poj 2689 区间素数筛

    The branch of mathematics called number theory is about properties of numbers. One of the areas that ...

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

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

  5. 大区间素数筛选(POJ 2689)

    /* *POJ 2689 Prime Distance *给出一个区间[L,U],找出区间内容.相邻的距离最近的两个素数和距离最远的两个素数 *1<=L<U<=2147483647 ...

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

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

  7. poj 2262 Goldbach's Conjecture(素数筛选法)

    http://poj.org/problem?id=2262 Goldbach's Conjecture Time Limit: 1000MS   Memory Limit: 65536K Total ...

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

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

  9. POJ 3978 Primes(素数筛选法)

    题目 简单的计算A,B之间有多少个素数 只是测试数据有是负的 //AC //A和B之间有多少个素数 //数据可能有负的!!! #include<string.h> #include< ...

随机推荐

  1. Linux从零到高手的进阶心得(转)

    从2006年毕业至今,从事IT行业已经接近8个年头. 一路走来有很多心路历程和技术心得都写在了51CTO的博客中,不少文字现在看来已显稚嫩,但是这正是我真实的成长之路.这八年,从最基础的网络管理员开 ...

  2. UDE-00008 ORA-31626 ORA-06512 ORA-25254

    今天在导出一个模式的时候,约140GB,出现例如以下错误: UDE-00008: operation generated ORACLE error 31626 ORA-31626: job does ...

  3. Android学习笔记(四十):Preference使用

    Preference从字面上看偏好,译为首选项. 一些配置数据,一些我们上次点击选择的内容.我们希望在下次应用调起的时候依旧有效,无须用户再一次进行配置或选择.Android提供preference这 ...

  4. 【Hibernate步步为营】--双向关联一对一映射具体解释(一)

    一对一的映射在对象模型中是常常见到的,为了将对象模型转换为关系模型就必须在映射文件里进行配置,上篇文章讨论了一对一映射的单向关联的情况,重点是<one-to-one>标签的使用,须要在映射 ...

  5. 为Linux用ISO制作U盘启动及基本原理

    制作成功后的基本最简文件夹文件图 一.系统的基本引导流程: 首先系统要引导isolinux.bin可执行程序,此程序是移动介质上引导用的,isolinux.bin执行成功后会载入其配置文件syslin ...

  6. MyEclipse建立JVM内存大小

    1.你为什么要设置JVM它的内存大小? 因为假定程序执行过JVM默认的内存大小.他们不会报告传说中的内存溢出异常. Exception in thread "main" java. ...

  7. LeetCode: Multiply Strings. Java

    Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...

  8. 【翻译自mos文章】回收 asm磁盘空间的方法

    回收 asm磁盘空间的方法 參考原文: How To Reclaim Asm Disk Space? (Doc ID 351866.1) 适用于: Oracle Database - Enterpri ...

  9. Android MVC MVP

    从.NET的宠物商店到Android MVC MVP   1 一些闲话 记得刚进公司的时候,我们除了做常规的Training Project外,每天还要上课,接受各种技术培训和公司业务介绍.当时第一次 ...

  10. C# WinForm多线程(二)ThreadPool 与 Timer

    本文接上文,继续探讨WinForm中的多线程问题,再次主要探讨threadpool 和timer 一  ThreadPool 线程池(ThreadPool)是一种相对较简单的方法,它适应于一些需要多个 ...