【POJ 2689】 Prime Distance
【题目链接】
http://poj.org/problem?id=2689
【算法】
我们知道,一个在区间[l,r]中的合数的最小质因子必然不超过sqrt(r)
那么,先暴力筛出1-50000中的质数,对于每个询问,用筛出的质数标记[l,r]中的合数,即可
【代码】
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cerrno>
#include <clocale>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <limits>
#include <list>
#include <map>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <utility>
#include <vector>
#include <cwchar>
#include <cwctype>
#include <stack>
#include <limits.h>
using namespace std;
#define MAXP 50000
#define MAXD 1000010
const int INF = 2e9; int i,j,last,mx,mn,l,r,L,U;
vector<int> P;
bool not_prime[MAXD];
pair<int,int> C,D;
bool flag; inline void init()
{
int i,j,tmp;
static int f[MAXP+];
for (i = ; i <= MAXP; i++)
{
if (!f[i])
{
P.push_back(i);
f[i] = i;
}
for (j = ; j < P.size(); j++)
{
tmp = i * P[j];
if (tmp > MAXP) break;
f[tmp] = P[j];
if (f[i] == P[j]) break;
}
}
} int main() { init();
while (scanf("%d%d",&L,&U) != EOF)
{
memset(not_prime,false,sizeof(not_prime));
flag = false;
last = -;
mx = ; mn = INF;
for (i = ; i < P.size(); i++)
{
if (L % P[i] == ) l = L / P[i];
else l = L / P[i] + ;
r = U / P[i];
for (j = max(l,); j <= r; j++) not_prime[P[i]*j-L] = true;
}
if (L == ) not_prime[] = true;
for (i = ; i <= U - L; i++)
{
if (!not_prime[i])
{
if (last == -)
{
last = i;
continue;
}
if (i - last < mn)
{
flag = true;
mn = i - last;
C = make_pair(last+L,i+L);
}
if (i - last > mx)
{
flag = true;
mx = i - last;
D = make_pair(last+L,i+L);
}
last = i;
}
}
if (flag) printf("%d,%d are closest, %d,%d are most distant.\n",C.first,C.second,D.first,D.second);
else printf("There are no adjacent primes.\n");
} return ; }
【POJ 2689】 Prime Distance的更多相关文章
- 一本通1619【例 1】Prime Distance
1619: [例 1]Prime Distance 题目描述 原题来自:Waterloo local,题面详见 POJ 2689 给定两个整数 L,R,求闭区间 [L,R] 中相邻两个质数差值最小的数 ...
- 【POJ - 3126】Prime Path(bfs)
Prime Path 原文是English 这里直接上中文了 Descriptions: 给你两个四位的素数a,b.a可以改变某一位上的数字变成c,但只有当c也是四位的素数时才能进行这种改变.请你计算 ...
- 【POJ 1741】Tree
Tree Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 11570 Accepted: 3626 Description ...
- 【POJ 2983】Is the Information Reliable?(差分约束系统)
id=2983">[POJ 2983]Is the Information Reliable? (差分约束系统) Is the Information Reliable? Time L ...
- bzoj 2295: 【POJ Challenge】我爱你啊
2295: [POJ Challenge]我爱你啊 Time Limit: 1 Sec Memory Limit: 128 MB Description ftiasch是个十分受女生欢迎的同学,所以 ...
- 【链表】BZOJ 2288: 【POJ Challenge】生日礼物
2288: [POJ Challenge]生日礼物 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 382 Solved: 111[Submit][S ...
- BZOJ2288: 【POJ Challenge】生日礼物
2288: [POJ Challenge]生日礼物 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 284 Solved: 82[Submit][St ...
- BZOJ2293: 【POJ Challenge】吉他英雄
2293: [POJ Challenge]吉他英雄 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 80 Solved: 59[Submit][Stat ...
- BZOJ2287: 【POJ Challenge】消失之物
2287: [POJ Challenge]消失之物 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 254 Solved: 140[Submit][S ...
随机推荐
- UVALIVE6886 Golf Bot (FFT)
题意:打高尔夫 给你n个距离表示你一次可以把球打远的距离 然后对于m个询问 问能否在两杆内把球打进洞 题解:平方一下就好 注意一下x0的系数为1表示打一杆 才发现数组应该开MAXN * 4 之前写的题 ...
- Linux常用命令——关机与重启命令
1.shutdown命令 shutdown [选项] 时间 --使用shutdown进行关机或重启会正确保存正在使用的服务,其他命令有一定的危险性,建议最好使用shutdown命令进行关机重启 选项: ...
- /etc/updatedb.conf配置文件
[root@localhost ~]# vi /etc/updatedb.conf PRUNE_BIND_MOUNTS = "yes" PRUNEFS = "9p afs ...
- XGBoost参数中文翻译以及参数调优
XGBoost:参数解释:https://blog.csdn.net/zc02051126/article/details/46711047 机器学习系列(11)_Python中Gradient Bo ...
- java8 foreach不能使用break、countinue
在学习1.8新特性的过程中,发现foreach中不可以使用break和countinue,然后我使用了return,结果如下图,对循环并没有影响. 百度一下,发现了一个不一样的回答 然后我就看了下源码 ...
- 前端安全 xss
整体的 XSS 防范是非常复杂和繁琐的,不仅需要在全部需要转义的位置,对数据进行对应的转义.而且要防止多余和错误的转义,避免正常的用户输入出现乱码. 虽然很难通过技术手段完全避免 XSS,但可以总结以 ...
- (C/C++学习)19.单目标遗传算法的C程序实现
说明:在学习生活中,经常会遇到各种各样的最优问题,其中最常见的就是求某个多维(多个自变量)函数在各个自变量各取何值时的最大值或最小值:例如求函数 f(x) = (x-5)2+(y-6)2+(z-7)2 ...
- Wind rotor states
test test Table of Contents 1. Wind rotor states 1.1. Turbulent Wake State 1.2. Vortex Ring State 1. ...
- c++行事准则
1.c++包含多种编程范式,每种范式遵循不同的准则: 2.除了#include和#ifdef之流,其他预处理器别用,多用enum和const: 3.函数声明定义多用const:
- openstack windows2012r2 glance镜像制作
镜像实现: 密码注入 修改密码 根分区扩展 1.下载windows iso镜像 下载地址:http://imsdn.com/MSDN-1.html 例如:cn_windows_server_2012_ ...