poj 2689 巧妙地运用素数筛选
称号:
给出一个区间[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 巧妙地运用素数筛选的更多相关文章
- poj 2689 (素数二次筛选)
Sample Input 2 17 14 17 Sample Output 2,3 are closest, 7,11 are most distant. There are no adjacent ...
- [ACM] POJ 2689 Prime Distance (筛选范围大素数)
Prime Distance Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12811 Accepted: 3420 D ...
- poj 2689 区间素数筛
The branch of mathematics called number theory is about properties of numbers. One of the areas that ...
- POJ 2689 Prime Distance(素数筛选)
题目链接:http://poj.org/problem?id=2689 题意:给出一个区间[L, R],找出区间内相连的,距离最近和距离最远的两个素数对.其中(1<=L<R<=2,1 ...
- 大区间素数筛选(POJ 2689)
/* *POJ 2689 Prime Distance *给出一个区间[L,U],找出区间内容.相邻的距离最近的两个素数和距离最远的两个素数 *1<=L<U<=2147483647 ...
- POJ 2689 Prime Distance (素数筛选法,大区间筛选)
题意:给出一个区间[L,U],找出区间里相邻的距离最近的两个素数和距离最远的两个素数. 用素数筛选法.所有小于U的数,如果是合数,必定是某个因子(2到sqrt(U)间的素数)的倍数.由于sqrt(U) ...
- poj 2262 Goldbach's Conjecture(素数筛选法)
http://poj.org/problem?id=2262 Goldbach's Conjecture Time Limit: 1000MS Memory Limit: 65536K Total ...
- poj 2689 Prime Distance(大区间素数)
题目链接:poj 2689 Prime Distance 题意: 给你一个很大的区间(区间差不超过100w),让你找出这个区间的相邻最大和最小的两对素数 题解: 正向去找这个区间的素数会超时,我们考虑 ...
- POJ 3978 Primes(素数筛选法)
题目 简单的计算A,B之间有多少个素数 只是测试数据有是负的 //AC //A和B之间有多少个素数 //数据可能有负的!!! #include<string.h> #include< ...
随机推荐
- JSP与ASP.PHP的比較
眼下,最经常使用的三种动态网页语言为:ASP(Active Server Pages).PHP(HyperText Preprocessor)及JSP(Java Server Pages). JSP与 ...
- Very Deep Convolutional Networks for Large-Scale Image Recognition
Very Deep Convolutional Networks for Large-Scale Image Recognition 转载请注明:http://blog.csdn.net/stdcou ...
- Locked ownable synchronizers(转)
public class DeadLock { public static void main(final String[] args) throws Exception { final Object ...
- C语言中main函数的參数具体解释
main函数的定义形式 main函数能够不带參数,也能够带參数,这个參数能够觉得是 main函数的形式參数.C语言规定main函数的參数仅仅能有两个,习惯上这两个參数写为argc和ar ...
- 【Android接口实现】ActionBar利用整理的一些细节
转载请注明出处:http://blog.csdn.net/zhaokaiqiang1992 关于ActionBar的使用,非常多大神早就已经写了非常好的文章进行了介绍,所以ActionBar的基本使用 ...
- MultiROM for the XIAOMI MI2S/2C/2! (Kexec HardBoot Enabled with Kexec HardBoot Patch!)
Introduction This is a port of Tassadar's MultiROM, a multi-boot mod for XIAOMI MI2/2S/2C. The main ...
- Win 10开门人类智慧的世界领先
3月18日,从微软硬件project大会(WinHEC 2015)上传来好消息:今年夏天,Win 10将要正式公布.Win 10公布,有何新意? 微软新领导人纳德拉(Nadella)主张:运计算,大数 ...
- 【读书札记】建立第一个Web项目
安装配置好jdk.tomcat,我用的版本号是7.0.54,我放在C:\server\apache-tomcat-7.0.54下, CATALINA_BASE:C:\server\apache-tom ...
- Cocos2d-x3.0 lua捆绑C++分类
我知道这个纪录Lua结合整个过程. 原文地址:http://blog.csdn.net/qqmcy/article/details/26099859 准备工作: 1.创一个一个Lua的2dxproje ...
- 什么是流利语法Fluent Syntax
出处:http://blog.csdn.net/u010019717 author:孙广东 时间:2015.3.18 23:00 编程新概念:什么是流利语法fluent synta ...