【题目链接】

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的更多相关文章

  1. 一本通1619【例 1】Prime Distance

    1619: [例 1]Prime Distance 题目描述 原题来自:Waterloo local,题面详见 POJ 2689 给定两个整数 L,R,求闭区间 [L,R] 中相邻两个质数差值最小的数 ...

  2. 【POJ - 3126】Prime Path(bfs)

    Prime Path 原文是English 这里直接上中文了 Descriptions: 给你两个四位的素数a,b.a可以改变某一位上的数字变成c,但只有当c也是四位的素数时才能进行这种改变.请你计算 ...

  3. 【POJ 1741】Tree

    Tree Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 11570   Accepted: 3626 Description ...

  4. 【POJ 2983】Is the Information Reliable?(差分约束系统)

    id=2983">[POJ 2983]Is the Information Reliable? (差分约束系统) Is the Information Reliable? Time L ...

  5. bzoj 2295: 【POJ Challenge】我爱你啊

    2295: [POJ Challenge]我爱你啊 Time Limit: 1 Sec  Memory Limit: 128 MB Description ftiasch是个十分受女生欢迎的同学,所以 ...

  6. 【链表】BZOJ 2288: 【POJ Challenge】生日礼物

    2288: [POJ Challenge]生日礼物 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 382  Solved: 111[Submit][S ...

  7. BZOJ2288: 【POJ Challenge】生日礼物

    2288: [POJ Challenge]生日礼物 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 284  Solved: 82[Submit][St ...

  8. BZOJ2293: 【POJ Challenge】吉他英雄

    2293: [POJ Challenge]吉他英雄 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 80  Solved: 59[Submit][Stat ...

  9. BZOJ2287: 【POJ Challenge】消失之物

    2287: [POJ Challenge]消失之物 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 254  Solved: 140[Submit][S ...

随机推荐

  1. Redis系列(三)--消息队列、排行榜等

    Redis命令执行生命周期: 发送命令--->排队(单线程)--->执行命令--->返回结果 慢查询: 只是针对命令执行阶段 慢查询日志通过一个固定长度的FIFO queue,这个q ...

  2. ThinkPHP---TP功能类之联表查询

    [一]介绍 在原生的sql中使用join 语法进行数据的联表查询, 在ThinkPHP里支持联表查询操作,但是可以归纳成两种方式:table方法.join方法 (1)table方法:在TP中对应SQL ...

  3. linux cp复制文件 直接覆盖

    命令: \cp -rf aaaa/* bbbb 复制aaa下的文件到bbb目录

  4. 'dict' object is not callable

    今天学py的map函数时,由于在上面定义了一个dict类型的变量(取的名是map),所以编译后报了这么一个错,哎,以后学py命名要小心了

  5. 模拟--P1427 小鱼的数字游戏

    题目描述 小鱼最近被要求参加一个数字游戏,要求它把看到的一串数字(长度不一定,以0结束,最多不超过100个,数字不超过2^32-1),记住了然后反着念出来(表示结束的数字0就不要念出来了).这对小鱼的 ...

  6. 洛谷——P3369 【模板】普通平衡树(splay)(基础splay,维护一些神奇的东东)

    P3369 [模板]普通平衡树 平衡树大法好,蒟蒻(博主)最近正在收集高级数据结构的碎片,企图合成数据结构的元素之力来使自己的RP++... 您需要写一种数据结构(可参考题目标题),来维护一些数,其中 ...

  7. linux top-显示或管理执行中的程序

    推荐:更多linux 性能监测与优化 关注:linux命令大全 top命令可以实时动态地查看系统的整体运行情况,是一个综合了多方信息监测系统性能和运行信息的实用工具.通过top命令所提供的互动式界面, ...

  8. angular中多个promise的合并处理

    all()方法 这个all()方法,可以把多个primise的数组合并成一个.当所有的promise执行成功后,会执行后面的回调.回调中的参数,是每个promise执行的结果.当批量的执行某些方法时, ...

  9. Spring security 5 Authorize Configuration

    1. Spring Security 核心请求,认证配置类 WebSecurityConfigurerAdapter protected void configure(HttpSecurity htt ...

  10. Windows窗口创建的具体步骤

    /*实现窗口创建的六步骤:第一步:创建入口函数WinMain第二步:注册窗口类第三部:实现回调函数的功能第四步:显示窗口第五步:更新窗口第六步:消息循环*/ #include "stdafx ...