称号:

给出一个区间[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. shell脚本中的数学运算

    shell中的赋值和操作默认都是字符串处理,在此记下shell中进行数学运算的几个特殊方法.以后用到的时候能够来看,呵呵 1.错误方法举例 a) var=1+1 echo $var 输出的结果是1+1 ...

  2. GRUB2配置详解:默认启动项,超时时间,隐藏引导菜单,配置文件详解,图形化配置

    配置文件详解: /etc/default/grub # 设定默认启动项,推荐使用数字 GRUB_DEFAULT=0 # 注释掉下面这行将会显示引导菜单 #GRUB_HIDDEN_TIMEOUT=0 # ...

  3. C++程序代写实现HashSet class

    C++程序代写实现HashSet class 专业程序代写(QQ:928900200) Implement a HashSet class for elements of type string.It ...

  4. javaweb学习总结(七)——HttpServletResponse对象(一)(转)

    Web服务器收到客户端的http请求,会针对每一次请求,分别创建一个用于代表请求的request对象.和代表响应的response对象. request和response对象即然代表请求和响应,那我们 ...

  5. UVA 674 (入门DP, 14.07.09)

     Coin Change  Suppose there are 5 types of coins: 50-cent, 25-cent, 10-cent, 5-cent, and 1-cent. We ...

  6. HTML5_表单元素

    <!DOCTYPE html> <hmtl> <html  lang="zh-cn"> <head> <meta charse ...

  7. js缓冲运动

    缓冲运动 现象:逐渐变慢,最后停止 原理:距离越远,速度越大 速度的计算方式: 1,速度由距离决定 2,速度=(目标值-当前值)/缩放系数 说明:速度为正负数时,也决定了物体移动的方向 示例:div缓 ...

  8. Java正则表达式例子汇总

    1.过滤特殊字符 package com.sheepmu.text; /* * @author sheepmu */ public class HWCompetition { public stati ...

  9. Lua 服务器Socket通信实例(转)

    local socket = require"socket" local host = "127.0.0.1"local port = "843&qu ...

  10. Solr在结果返回函数值

    如果你有一个服务,你的用户能够搜索不同的公司.用户输入一个简单的keyword,能够返回匹配keyword的公司,可是有一天,用户要求返回的公司列表中能够显示公司跟用户的距离,这是该怎么做呢? 1.如 ...