UVA10140 Prime Distance【素数/数论】By cellur925
我们注意到,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的更多相关文章
- [POJ268] Prime Distance(素数筛)
/* * 二次筛素数 * POJ268----Prime Distance(数论,素数筛) */ #include<cstdio> #include<vector> using ...
- UVA10140 Prime Distance
UVA10140 Prime Distance 给定两个整数L,R(1<=L<=R<=2^{31},R-L<=10^6)L,R(1<=L<=R<=231,R− ...
- 【题解】UVA10140 [Prime Distance]
[题解]UVA10140 Prime Distance 哈哈哈哈\(miller-rabbin\)水过去了哈哈哈 还能怎么办呢?\(miller-rabbin\)直接搞.枚举即可,还跑得飞快. 当然此 ...
- POJ2689 Prime Distance(数论:素数筛选模板)
题目链接:传送门 题目: Prime Distance Time Limit: 1000MS Memory Limit: 65536K Total Submissions: Accepted: Des ...
- ZOJ 1842 Prime Distance(素数筛选法2次使用)
Prime Distance Time Limit: 2 Seconds Memory Limit: 65536 KB The branch of mathematics called nu ...
- POJ2689 - Prime Distance(素数筛选)
题目大意 给定两个数L和U,要求你求出在区间[L, U] 内所有素数中,相邻两个素数差值最小的两个素数C1和C2以及相邻两个素数差值最大的两个素数D1和D2,并且L-U<1,000,000 题解 ...
- 数论 - 素数的运用 --- poj 2689 : Prime Distance
Prime Distance Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12512 Accepted: 3340 D ...
- poj 2689 Prime Distance (素数二次筛法)
2689 -- Prime Distance 没怎么研究过数论,还是今天才知道有素数二次筛法这样的东西. 题意是,要求求出给定区间内相邻两个素数的最大和最小差. 二次筛法的意思其实就是先将1~sqrt ...
- UVA 10140 - Prime Distance(数论)
10140 - Prime Distance 题目链接 题意:求[l,r]区间内近期和最远的素数对. 思路:素数打表,打到sqrt(Max)就可以,然后利用大的表去筛素数.因为[l, r]最多100W ...
随机推荐
- Safair浏览器 时间戳转化兼容性问题。
chrome 等浏览器支持 yyyy-MM-dd hh:mm:ss 格式,使用 Date.parse()进行转化 safair 浏览器不知道这种格式,需要将格式设置为 yyyy/MM/dd hh:mm ...
- POJ1456 Supermarket —— 贪心 + 路径压缩优化
题目链接:http://poj.org/problem?id=1456 Supermarket Time Limit: 2000MS Memory Limit: 65536K Total Subm ...
- Java GET和POST请求
从表面来看GET和POST请求: GET请求是在url后直接附上请求体,url和请求体之间用"?"分割,不同参数之间用"&"分隔,%XX中的XX为该符号 ...
- 步入C编程的第一天
我想学ruby以后开发网站,但ruby是高级语言,隐藏了许多底层的东西,因此先熟悉c语言 首先c程序的文件名是以.c结尾的 c程序的格式: 第一行#include<stdio.h> #是一 ...
- Windows下Tesseract4.0识别与中文手写字体训练
一 . tesseract 4.0 安装及使用 1. tesseract 4.0 安装 安装包下载地址: http://digi.bib.uni-mannheim.de/tesseract/tesse ...
- [LeetCode] Shortest Distance from All Buildings Solution
之前听朋友说LeetCode出了一道新题,但是一直在TLE,我就找时间做了一下.这题是一个比较典型的BFS的题目,自己匆忙写了一个答案,没有考虑优化的问题,应该是有更好的解法的. 原题如下: You ...
- Relative atomic mass
Relative atomic mass Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Oth ...
- 深度学习网络结构中超参数momentum了解
训练网络时,通常先对网络的初始权值按照某种分布进行初始化,如:高斯分布.初始化权值操作对最终网络的性能影响比较大,合适的网络初始权值能够使得损失函数在训练过程中的收敛速度更快,从而获得更好的优化结果. ...
- 关闭页面,window.onunload事件未执行的原因
1.问题描述: JS中定义widow.onunload= function(),页面关闭时,logout()函数未执行. window.onunload = function() { logout() ...
- html格式
优美的代码编写方式是我们装逼的基础,在python中我们称优秀的代码为pythonic,无独有偶,html.css.js也都有着自己相比更优美的写法~ <!DOCTYPE html> &l ...