最近改自己的错误代码改到要上天,心累。

这是迄今为止写的最心累的博客。

Prime Distance

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 18707   Accepted: 4998

Description

The branch of mathematics called number theory is about properties of numbers. One of the areas that has captured the interest of number theoreticians for thousands of years is the question of primality. A prime number is a number that is has no proper factors (it is only evenly divisible by 1 and itself). The first prime numbers are 2,3,5,7 but they quickly become less frequent. One of the interesting questions is how dense they are in various ranges. Adjacent primes are two numbers that are both primes, but there are no other prime numbers between the adjacent primes. For example, 2,3 are the only adjacent primes that are also adjacent numbers.
Your program is given 2 numbers: L and U (1<=L< U<=2,147,483,647), and you are to find the two adjacent primes C1 and C2 (L<=C1< C2<=U) that are closest (i.e. C2-C1 is the minimum). If there are other pairs that are the same distance apart, use the first pair. You are also to find the two adjacent primes D1 and D2 (L<=D1< D2<=U) where D1 and D2 are as distant from each other as possible (again choosing the first pair if there is a tie).

Input

Each line of input will contain two positive integers, L and U, with L < U. The difference between L and U will not exceed 1,000,000.

Output

For each L and U, the output will either be the statement that there are no adjacent primes (because there are less than two primes between the two given numbers) or a line giving the two pairs of adjacent primes.

Sample Input

  2 17

  14 17

Sample Output

 2,3 are closest, 7,11 are most distant.

 There are no adjacent primes.

题意就是找给定的区间内距离最近的两个素数和距离最远的两个素数。
因为给的数很大,不能从(1-U)筛素数,这样写会re,我是智障,我一开始从1到U开始筛的素数,被T飞了,
想着自己好不容易改了个筛选法求欧拉函数的代码(改的这个代码,传送门:http://www.cnblogs.com/ZERO-/p/6582239.html)写这道题,就一直坚持不懈的改。

样例可以过啊,交上就不可以。后来越改越不靠谱,Memory Limit Exceeded,Runtime Error。。。
这才反应过来是自己的代码写的有问题,要换思路。因为给的数很大,所以从头开始筛素数是不可以的,空间上不允许,所以才MLE。。。
然后就看了一下区间筛素数,把人家求区间有几个素数的代码拿来改(传送门:http://www.cnblogs.com/nowandforever/p/4515612.html),改的我头大,但好在最后a了,唉,心累啊。
人家的题目代码:

给定整数a和b,请问区间[a,b)内有多少个素数?

a<b<=10^12

b-a<=10^6

因为b以内合数的最小质因数一定不超过sqrt(b),如果有sqrt(b)以内的素数表的话,就可以把筛选法用在[a,b)上了,先分别做好[2,sqrt(b))的表和[a,b)的表,然后从[2,sqrt(b))的表中筛得素数的同时,也将其倍数从[a,b)的表中划去,最后剩下的就是区间[a,b)内的素数了。

有的时候需要求出某个特定区间的素数,但是数可能很大,数组也开不小,所以需要进行下标偏移,这样才可以使用筛选法。

区间素数统计代码:

 #include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const int maxn=1e6+;
bool is_prime[maxn];
bool is_prime_small[maxn];
ll prime[maxn];
ll prime_num=;
void segment_sieve(ll a,ll b){
for(ll i=;i*i<b;++i) is_prime_small[i]=true;
for(ll i=;i<b-a;++i) is_prime[i]=true;
for(ll i=;i*i<b;++i){
if(is_prime_small[i]){
for(ll j=*i;j*j<b;j+=i) is_prime_small[j]=false;
for(ll j=max(2LL,(a+i-)/i)*i;j<b;j+=i) is_prime[j-a]=false;
}
}
for(ll i=;i<b-a;++i)
if(is_prime[i]) prime[prime_num++]=i+a;
}
int main(){
ll a,b;
while(~scanf("%lld%lld",&a,&b)){
prime_num=;
memset(prime,,sizeof(prime));
segment_sieve(a,b);
printf("%lld\n",prime_num);
}
return ;
}

好好理解了人家的代码之后就开始进行改造了。

人家的代码写的是有下标偏移量的,所以后来把素数找出来的时候要再加回去,就是i+n;

然后就是将距离最近的值和距离最远的值找出来,然后再通过距离值找出来素数,应该有直接就可以将素数找出来的,无奈,我太菜,只会这样写。。。

因为人家是求的[a,b)区间的素数个数,所以我改的时候就写的a,b+1,这样求的就是[a,b]闭区间的素数,然后就是人家的代码没有将1这个既不是素数也不是合数的家伙去掉,所以加了个判断。

自己的垃圾代码:

 #include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N=1e6+;
const ll INF=0x3f3f3f3f;
bool prime1[N];
bool prime2[N];
ll ss[N];
ll num=;
void findprime(ll n,ll m){ //区间筛素数,筛的是从[n,m)的,下面传的参数是n,m+1,所以保证是求的闭区间的。好好想想。
for(ll i=;i*i<m;i++) prime1[i]=true; //这是对[2,sqrt(m))的初始化。
for(ll i=;i<m-n;i++) prime2[i]=true; //对下标偏移之后的[n,m)进行初始化。
for(ll i=;i*i<m;i++){
if(prime1[i]){
for(ll j=*i;j*j<m;j+=i) prime1[j]=false; //找出来[2,sqrt(m))中不是素数的筛出去。
for(ll j=max((ll),(n+i-)/i)*i;j<m;j+=i) prime2[j-n]=false; //将偏移后的[n,m)中的不是素数的筛出去。
} //j=max((ll)2,(n+i-1)/i)*i;j<m;j+=i)意思就是从最接近n的数开始,将那些合数的倍数,从2倍开始,筛掉。
}
}
int main(){
ll n,m,h;
ll minn,maxx;
while(~scanf("%lld%lld",&n,&m)){
memset(prime2,,sizeof(prime2));
memset(ss,,sizeof(ss));
findprime(n,m+); //这样保证是求的[n,m]这个闭区间的
h=;
for(int i=;i<m-n+;i++){
if(prime2[i]&&i+n!=)//如果是true就是成立的,因为上面区间筛素数没有筛掉1(既不是素数也不是合数),所以判断一下。
ss[h++]=i+n; //将下标偏移量再加回来。
}
if(h<) printf("There are no adjacent primes.\n"); //保证必须有>=2个素数。
else{
minn=INF;maxx=-;
for(int i=;i<h-;i++){ //我太菜,只会这样找。
minn=min(minn,ss[i+]-ss[i]); //找出距离最近的值是多少。
maxx=max(maxx,ss[i+]-ss[i]); //找出距离最远的值是多少。
}
for(int i=;i<h-;i++){
if(ss[i+]-ss[i]==minn){ //将距离最近的2个素数找出来,输出来最先找到的一组就可以。
printf("%lld,%lld are closest, ",ss[i],ss[i+]);
break;
}
}
for(int i=;i<h-;i++){ //意思同上。
if(ss[i+]-ss[i]==maxx){
printf("%lld,%lld are most distant.\n",ss[i],ss[i+]);
break;
}
}
}
}
return ;
}

改代码改的好辛苦,因为太菜了,人家补题的时间,我就只能自己一个人怀疑人生般的debug。。。

改了20多遍,我也是菜的可怜。。。

筛选法求欧拉函数的代码也能让我改成从2开始到n筛素数的代码,感觉自己也是没谁了,呵呵哒。

这就是现实,自己满心欢喜的改自己好不容易写出来的代码,最后也是不可以,那就放弃换一个思路吧。总得做点什么,要不怎么都a不了这道题。

反省深刻,继续努力,菜的掉渣,咸鱼,加油哦。唉:-(

剩下的题解等把所有的题都补完了再来写。

POJ2689-Prime Distance-区间筛素数的更多相关文章

  1. POJ-2689 Prime Distance,区间素数筛法

                                                    Prime Distance 只会埃氏筛法的弱鸡今天读了读挑战程序设计120页,明白了求小区间内素数的方 ...

  2. poj 2689 Prime Distance(区间筛选素数)

    Prime Distance Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9944   Accepted: 2677 De ...

  3. POJ - 2689 Prime Distance (区间筛)

    题意:求[L,R]中差值最小和最大的相邻素数(区间长度不超过1e6). 由于非素数$n$必然能被一个不超过$\sqrt n$的素数筛掉,因此首先筛出$[1,\sqrt R]$中的全部素数,然后用这些素 ...

  4. POJ 2689.Prime Distance-区间筛素数

    最近改自己的错误代码改到要上天,心累. 这是迄今为止写的最心累的博客. Prime Distance Time Limit: 1000MS   Memory Limit: 65536K Total S ...

  5. POJ-2689 Prime Distance (两重筛素数,区间平移)

    Prime Distance Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13961   Accepted: 3725 D ...

  6. poj 2689 Prime Distance(大区间筛素数)

    http://poj.org/problem?id=2689 题意:给出一个大区间[L,U],分别求出该区间内连续的相差最小和相差最大的素数对. 由于L<U<=2147483647,直接筛 ...

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

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

  8. poj2689Prime Distance(大区间筛素数)

    Prime Distance Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 19635   Accepted: 5273 D ...

  9. 解题报告:poj2689 Prime Distance

    2017-10-03 11:29:20 writer:pprp 来源:kuangbin模板 从已经筛选好的素数中筛选出规定区间的素数 /* *prime DIstance *给出一个区间[L,U],找 ...

  10. poj2689 Prime Distance题解报告

    题目戳这里 [题目大意] 给定一个区间[L,R],求区间内的质数相邻两个距离最大和最小的. [思路分析] 其实很简单呀,很明显可以看出来是数论题,有关于质数的知识. 要注意一下的就是L和R的数据范围都 ...

随机推荐

  1. 函数的非固定参数-Day3

    一.函数非固定参数 1.默认函数,我们在传参之前,选给参数指定一个默认的值.默认参数特点是非必须传递的. def test(x,y=2): print(x) print(y) print(" ...

  2. 视觉SLAM的数学表达

    相机是在某些时刻采集数据的,所以只关心这些时刻的位置和地图. 就把这一段时间的运动变成了李三时刻 t=1,2,...K当中发生的事情. 在这些事可,x表示机器自身的位置. x1,x2,x3,x4... ...

  3. openstack操作之一 命令行

    在openstack环境中提供了多种操作虚拟机的方法,有最简单直接的dashborad界面,有不直观但高效的命令行,还有进阶版的postman调用openstack restfulapi和命令行中使用 ...

  4. WPF单位真的与分辨率无关吗?

    转载自http://www.cnblogs.com/helloj2ee/archive/2009/04/21/1440709.htm WPF从发布之日起,一直将"分辨率无关(resoluti ...

  5. lesson - 9 课程笔记

    一. yum          作用:                     yum 命令是在Fedora 和RedHat 以及SUSE 中基于rpm 的软件包管理器,它可以使系统管理人员交互和自动 ...

  6. RedisPool操作Redis,工具类实例

    redis.properties 配置文件内容 redis.pool.maxActive=100redis.pool.maxIdle=20redis.pool.maxWait=3000redis.po ...

  7. JavaScript函数(二)

    在前面我们已经对函数作了简单的介绍,比如函数的定义.函数的声明.函数的调用和函数的传参等.本节将进一步介绍函数的应用,深度理解函数的各种使用. 函数是一个对象,每个函数时Function类型的一个实例 ...

  8. UWP 应用通知Notifications

    之前说UWP 使用OneDrive云存储2.x api(二)[全网首发],微识别实现了上传下载的功能,那么为了给用户更上一层楼的体验,那就是在上传下载完成之后,弹出一通知Notifications. ...

  9. Less is exponentially more

    Less is exponentially more  (原文出处:rob pike 博客,https://commandcenter.blogspot.jp/2012/06/less-is-expo ...

  10. docker 保存 加载(导入 导出镜像

    tensorflow 的docker镜像很大,pull一次由于墙经常失败.其实docker 可以将镜像导出再导入. 保存加载(tensorflow)镜像 1) 查看镜像 docker images 如 ...