题目传送门

我们注意到,L,R是肥肠大的.........我们不可能在1s内筛出2^31内的全部质数。

“上帝为你关上一扇门,同时为你打开一扇窗”

我们又注意到,R-L是肥肠比较小的,珂以从这入手解决问题。

我们知道,任意一个合数x一定包含不超过sqrt(n)的质因子。

所以我们就筛出2~sqrt(R)之间的所有素数,用他们来标记全部范围内的合数。最后没被标记的数就是质数,比较相邻的质数位置取最大。

Code

 #include<cstdio>
#include<cmath>
#include<algorithm>
#include<iostream>
#include<cstring> using namespace std; int zz,xx,gg,bb,tot,l,r,cnt,minn,maxx;
bool v[];
int pri[],b[]; bool prime(int q)
{
if(q==||q==) return ;
if(q==) return ;
if(q%!=&&q%!=) return ;
int Vergil=sqrt(q);
for(int i=;i<=Vergil;i+=)
if(q%i==||q%(i+)==) return ;
return ;
} void pre()
{
for(int i=;i<=;i++)
if(prime(i)) pri[++tot]=i;
} int main()
{
pre();
while(cin>>l>>r)
{
memset(v,,sizeof(v));cnt=;
if(l==) v[]=;
for(int i=;i<=tot;i++)
for(int j=l/pri[i];j<=r/pri[i];j++)
if(j>) v[j*pri[i]-l]=;//"-l"是在节省空间
for(int i=l;i<=r;i++)
{
if(v[i-l]) b[++cnt]=i;
if(i==r) break;
} minn=,maxx=;
for(int i=;i<=cnt-;i++)
{
int tmp=b[i+]-b[i];
if(tmp<minn) zz=b[i],xx=b[i+],minn=tmp;
if(tmp>maxx) gg=b[i],bb=b[i+],maxx=tmp;
}
if(!maxx)
printf("There are no adjacent primes.\n");
else printf("%d,%d are closest, %d,%d are most distant.\n",zz,xx,gg,bb);
}
return ;
}

UVA10140 Prime Distance【素数/数论】By cellur925的更多相关文章

  1. [POJ268] Prime Distance(素数筛)

    /* * 二次筛素数 * POJ268----Prime Distance(数论,素数筛) */ #include<cstdio> #include<vector> using ...

  2. UVA10140 Prime Distance

    UVA10140 Prime Distance 给定两个整数L,R(1<=L<=R<=2^{31},R-L<=10^6)L,R(1<=L<=R<=231,R− ...

  3. 【题解】UVA10140 [Prime Distance]

    [题解]UVA10140 Prime Distance 哈哈哈哈\(miller-rabbin\)水过去了哈哈哈 还能怎么办呢?\(miller-rabbin\)直接搞.枚举即可,还跑得飞快. 当然此 ...

  4. POJ2689 Prime Distance(数论:素数筛选模板)

    题目链接:传送门 题目: Prime Distance Time Limit: 1000MS Memory Limit: 65536K Total Submissions: Accepted: Des ...

  5. ZOJ 1842 Prime Distance(素数筛选法2次使用)

    Prime Distance Time Limit: 2 Seconds      Memory Limit: 65536 KB The branch of mathematics called nu ...

  6. POJ2689 - Prime Distance(素数筛选)

    题目大意 给定两个数L和U,要求你求出在区间[L, U] 内所有素数中,相邻两个素数差值最小的两个素数C1和C2以及相邻两个素数差值最大的两个素数D1和D2,并且L-U<1,000,000 题解 ...

  7. 数论 - 素数的运用 --- poj 2689 : Prime Distance

    Prime Distance Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12512   Accepted: 3340 D ...

  8. poj 2689 Prime Distance (素数二次筛法)

    2689 -- Prime Distance 没怎么研究过数论,还是今天才知道有素数二次筛法这样的东西. 题意是,要求求出给定区间内相邻两个素数的最大和最小差. 二次筛法的意思其实就是先将1~sqrt ...

  9. UVA 10140 - Prime Distance(数论)

    10140 - Prime Distance 题目链接 题意:求[l,r]区间内近期和最远的素数对. 思路:素数打表,打到sqrt(Max)就可以,然后利用大的表去筛素数.因为[l, r]最多100W ...

随机推荐

  1. Dynamic Web Module to 3.0 报错

    一.问题 使用maven项目创建的webapp项目Dynamic Web Module 默认是2.3. 当我们要切换到3.0的时候出现这个错误. 二.解决 1.点击进入Navigator view ( ...

  2. STM32 ~ 如何从ST网站找到对应的固件库

    ST官方网站改版后,基本上很难搜索到固件库的地址,找了半天才找到固件库的下载地址,通过此方法可以找到其他需要的资源,故记下来方便大家. 下载的网站地址为: Home>Tools and Soft ...

  3. 剑指Offer:旋转数组的最小数字【11】

    剑指Offer:旋转数组的最小数字[11] 题目描述 把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转. 输入一个非递减排序的数组的一个旋转,输出旋转数组的最小元素. 例如数组{3,4 ...

  4. zabbix 用户自定义监控参数添加

    1. item  key的添加 key可以带参数,该参数为一个数组列表,可以同时传递多个参数,key的格式如下 key -- [ parameters] -- 例如: vfs.fs.size[/] v ...

  5. BluetoothLE-Multi-Library 一个能够连接多台蓝牙设备的库,它可以作为client端,也可以为server端。支持主机/从机,外围设备连接。

    github地址:https://github.com/qindachang/BluetoothLE-Multi-Library BluetoothLE-Multi-Library 一个能够连接多台蓝 ...

  6. linux应用之vi编辑器的安装、配置及用法

    vi(vim是其高级版本)是linux系统上用于文本编辑的一个应用.它的功能十分强大,在日常的系统管理活动或编程中用得都很多.所以用好vi是很有必要的. 学习vi主要学的知识点有:1.vi的配置.2. ...

  7. Gradle build-info.xml not found for module app.Please make sure that you are using gradle plugin '2.0.0-alpha4' or higher.

    解决方法:去掉“Enable Instant run to host swap code/resource changes on deploy(default enabled)”的勾选项 Settin ...

  8. tcpdump 探测器分析

    注:默认情况下,tcpdump临听它遇见的第一个网络接口,如果它选择了错误的接口,可以-i标志强行指定接口,如果DNS不能用,或者只是不希望tcpdump进行名字查找,请使用-n选项,这个选项(-n) ...

  9. ubuntu16.04 NVIDIA 驱动安装

    查看驱动版本号 查看驱动适用版本:NVIDIA驱动版本查询 查看显卡对应的驱动版本: 举例如下: 禁止集成的nouveau驱动 Ubuntu系统集成的显卡驱动程序是nouveau,它是第三方为NVID ...

  10. webpack 知识点总结

    1 webpack 的不同点: 1.1 实现代码分割 1.2 loaders 1.3 plugin system 2 webpack 打包css,需要使用css-loaders 使webpack识别c ...