题目传送门

我们注意到,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. 九度OJ 1091:棋盘游戏 (DP、BFS、DFS、剪枝)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:1497 解决:406 题目描述: 有一个6*6的棋盘,每个棋盘上都有一个数值,现在又一个起始位置和终止位置,请找出一个从起始位置到终止位置代 ...

  2. 负载均衡实现,一个域名对应多个IP地址

    负载均衡实现,一个域名对应多个IP地址 - 宏宇 - 博客园 https://www.cnblogs.com/cuihongyu3503319/archive/2012/07/09/2583129.h ...

  3. Splits a tensor into sub tensors

    https://www.tensorflow.org/api_docs/python/tf/split # 'value' is a tensor with shape [5, 30] # Split ...

  4. OI中字符串读入和处理

    OI中字符串读入和处理 在NOIP的"大模拟"题中,往往要对字符串进行读入并处理,这些字符串有可能包含空格并以\n作为分割,传统的cin >> scanf() 等等,不 ...

  5. leetcode 750. Number Of Corner Rectangles

    Given a grid where each entry is only 0 or 1, find the number of corner rectangles. A corner rectang ...

  6. 绝对定位(absolute)

    绝对定位(absolute),作用是将被赋予此定位方法的对象从文档流中拖出,使用left,right,top, bottom等属性相对于其最接近的一个最有定位设置的父级对象进行绝对定位,如果对象的父级 ...

  7. c语言之秒数算法

    // 水仙花树:是指一个3位数字,立方和 等于该数本身 // 秒数算法:随便输入一个大于0的数,求出对应的多少小时多少分钟多少秒 #include <stdio.h> / int main ...

  8. tomcat正常启动,但是java项目没有启动原因

    右键项目,选择properties,查看该属性配置的是否正确

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

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

  10. npm 引入第三方过滤器

    根据项目具体情况使用过滤器,如果不满足业务需求可以在vue的全局或者局部自定义fiter(过滤器) 一下是github提供的第三方过滤器: https://github.com/freearhey/v ...