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 ...
随机推荐
- 九度OJ 1122:吃糖果 (递归)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:1522 解决:1200 题目描述: 名名的妈妈从外地出差回来,带了一盒好吃又精美的巧克力给名名(盒内共有 N 块巧克力,20 > N ...
- 九度OJ 1098:字母统计 (计数)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:3720 解决:1809 题目描述: 输入一行字符串,计算其中A-Z大写字母出现的次数 输入: 案例可能有多组,每个案例输入为一行字符串. 输 ...
- Automating hybrid apps
Automating hybrid apps One of the core principles of Appium is that you shouldn’t have to change you ...
- Android开发之onMeasure(int widthMeasureSpec, int heightMeasureSpec)方法
onMeasure()函数由包含这个View的具体的ViewGroup调用,因此值也是由其ViewGroup中传入的.子类View的这两个参数widthMeasureSpec, heightMeasu ...
- RAC 单节点实例异常关闭,关键报错ORA--29770
监控系统监控到RAC 的一个实例异常关闭 ,时间是凌晨1点多,还好没有影响到业务 之后就是分析原因 这套RAC搭建在虚拟化环境OS SUSE11 查看oracel alert log信息 Mon :: ...
- Android图片加载神器之Fresco, 基于各种使用场景的讲解
Fresco是Facebook开源Android平台上一个强大的图片加载库,也是迄今为止Android平台上最强大的图片加载库. 优点:相对于其他开源的第三方图片加载库,Fresco拥有更好的内存管理 ...
- Unable to resolve target 'android-16'
Just now when I imported the "android-support-v7-appcompat" to ADT,the console pointed out ...
- bootStrap效果图
http://www.ziqiangxuetang.com/bootstrap/bootstrap-tutorial.html
- android性能测试工具
Android性能测试工具Emmagee介绍 Emmagee介绍 Emmagee是监控指定被测应用在使用过程中占用机器的CPU.内存.流量资源的性能测试小工具.该工具的优势在于如同windows系 ...
- 4月超棒的JavaScript游戏开发框架推荐(1) – 51CTO.COM
基于JavaScript开发的游戏是唯一一个能够跨桌面,Web和移动三种平台的.… 查阅全文 ›