题目传送门

我们注意到,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. 判断一个IP地址是否是本局域网内地址

    //        /// <summary>        /// 判断一个IP地址是否是本局域网内地址,是返回true 否则返回false,        /// </summa ...

  2. go网关

    package main import ( "flag" "fmt" "io" "net" "os" ...

  3. 在VC++空工程中使用MFC类,采用Unicode字符集后,运行工程程序报错的解决方案

    创建一个VC++空工程,将Project Properties->General->Use of MFC改为Use MFC in a Shared DLL 新建一个源文件,内容如下 #in ...

  4. cc 和gcc编译器

    从名字上看,老的unix系统的CC程式叫做C Compiler.但GCC这个名字按GNU的说法叫做Gnu Compiler Collection.因为gcc包含非常多编译器(C, C++, Objec ...

  5. JavaScript学习第三天

    今天学习第三天. 凡事都是需要坚持的,坚持下去. 学习内容: 1.document.getElementById(""),document.getElementByTagName( ...

  6. Ubuntu环境下配置Android Studio【转】

    本文转载自:https://www.jianshu.com/p/1f6295f9c955 之前学习Android开发的时候,一直跟各种教程一样,使用的是Eclipse+ADT,主要是比较方便,容易上手 ...

  7. Android图片加载神器之Fresco-加载图片基础[详细图解Fresco的使用](秒杀imageloader)

    Fresco简单的使用—SimpleDraweeView 百学须先立志—学前须知: 在我们平时加载图片(不管是下载还是加载本地图片…..)的时候,我们经常会遇到这样一个需求,那就是当图片正在加载时应该 ...

  8. 看 迪杰斯特拉(Dijsktra)算法体会

    迪杰斯特拉 看啊哈算法中迪杰斯特拉算法体会: 算法思路 : 1.先找到源头到其他点的最短路: 2.以最短路作为中转点进行比较,用一个dis数组保存源头到他的最优距离 3.用循环进行最优筛选: #inc ...

  9. smali语法积累记录

    1.constructor 我们知道运行一个类的时候会先调用static方法中的内容,比如: static { System.loadLibrary("qihooTest"); } ...

  10. 「NOIP2014」「Codevs3728」 联合权值(乱搞

    3728 联合权值 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold   题目描述 Description 输入描述 Input Description 输出描述 Ou ...