称号:

给出一个区间[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. Steps UVA 846

    说说:此题要求求出从整数x到达整数y所要经过的最短步数,且第一步和最后一步必须为一,同一时候每一步都比前一步多一步,少一步或一样.如果想搞清楚每一步详细是如何走的,那么这道题是相当麻烦的.考虑到前后两 ...

  2. Android在ExpandableListView控制的基本使用

    在本文中,Demo为了展示Android在ExpandableListView用途管制.如该组/儿子ListView绑定数据源. 直接上代码例如以下: 程序结构图: layout文件夹下的 main. ...

  3. UVA 11427 - Expect the Expected(概率递归预期)

    UVA 11427 - Expect the Expected 题目链接 题意:玩一个游戏.赢的概率p,一个晚上能玩n盘,假设n盘都没赢到总赢的盘数比例大于等于p.以后都不再玩了,假设有到p就结束 思 ...

  4. AndroidUI的组成部分GridView

    java 代码例如以下(简单的知识点我会以凝视的形式解说): package com.gc.gridviewdemo; /** * @author Android将军 */ /** * 知识点解说: ...

  5. HTML5 在canvas绘制一个矩形

    笔者:本笃庆军 原文地址:http://blog.csdn.net/qingdujun/article/details/32930501 一.绘制矩形 canvas使用原点(0,0)在左上角的坐标系统 ...

  6. windows任务设置定时

    windows 的Schedule Task .创建一个配置 1.点击"开始" 2.点击"控制面板" 3.双击"任务计划程序" 4.双击&q ...

  7. Quartz CronTrigger应用

    CronTrigger配置格式: 格式: [第二] [支] [小时] [日本] [月] [周] [年]  序号 说明  是否必填  同意填写的值 同意的通配符  1  秒  是  0-59    , ...

  8. Android架构分析之LOG模块

    作者:刘昊昱 博客:http://blog.csdn.net/liuhaoyutz Android版本:2.3.7_r1 Linux内核版本:android-goldfish-2.6.29 Andro ...

  9. 【原创】leetCodeOj --- Find Minimum in Rotated Sorted Array II 解题报告

    题目地址: https://oj.leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/ 题目内容: Suppose a sort ...

  10. jQuery照片伸缩效应,这不是一个简单的图像缩放,它不影响其它元素的布局

    之前在网上看到这样的效果,但我没有收藏夹网址,后来被我不知道如何来实现这种效果. 如今,互联网已收集有关专门.真是功夫不负有心人,被我发现. 我也努力过自己尝试着写: 但仅仅是单纯的图片放大.并且还影 ...