【题目链接】

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. CAD使用SetXData写数据(com接口)

    主要用到函数说明: MxDrawEntity::SetXData 设置实体的扩展数据,详细说明如下: 参数 说明 [in] IMxDrawResbuf* pXData 扩展数据链表 c#代码实现如下: ...

  2. pringboot开启找回Run Dashboard

    代码中加入 <option name="configurationTypes"> <set> <option value="SpringBo ...

  3. Oracle 11g 字符集修改

    服务端字符集修改 1.确认服务端字符集 select userenv('language') from dual; 2.修改服务端字符集 首先以 DBA 身份登录 Oracle.Windows 系统下 ...

  4. 「 Luogu P2285 」打鼹鼠

    解题思路 第一眼看上去觉得要设计一个三维的 DP,$dp[i][j][k]$ 表示在 $(i,j)$ 这个位置上 $k$ 时刻能够打死的最多的鼹鼠. 但是被数据范围卡死.完全开不开数组啊. 然后注意到 ...

  5. WIndows 系统下的常用命令 和 检测方法

    ### 一.检测硬盘速度(Windows 自带工具) #### 使用windows 系统自带的工具测试硬盘读写速度 > 在使用下面命令前,需要获得管理员权限,才会在Dos窗口上显示(否则,一闪而 ...

  6. [Algorithm] 7. Serialize and Deserialize Binary Tree

    Description Design an algorithm and write code to serialize and deserialize a binary tree. Writing t ...

  7. TestNG套件测试(二)

    在xml中指定要运行的整个包来执行套件测试 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE ...

  8. java导出word的6种方式(转发)

    来自: http://www.cnblogs.com/lcngu/p/5247179.html 最近做的项目,需要将一些信息导出到word中.在网上找了好多解决方案,现在将这几天的总结分享一下. 目前 ...

  9. 洛谷 2471 BZOJ 1067 [SCOI2007]降雨量

    [题解] 用线段树维护区间最大值(因为没有修改,St表也可以),然后由于x,y可能是降雨量未知的年份,需要进行分类讨论. #include<cstdio> #include<algo ...

  10. 分金币 (UVA 11300)

    http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=33899 思路:推公式,发现可以转化为求给定n个数,求到所有点距离之和最小的点 ...