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

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

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不了这道题。

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

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

最新文章

  1. Beam Search(集束搜索/束搜索)
  2. HDU 5950 矩阵快速幂
  3. 解决VS2013/VS2010简体中文版无法使用ReSharper快捷键的问题
  4. [转载]MongoDB开发学习(1)开天辟地,经典入门
  5. C++ Prime:sizeof运算符
  6. css selector: xpath:
  7. js 冒泡排序
  8. extern “ C”的含义
  9. OI大佬博客集
  10. pthread线程属性介绍
  11. 文档对象模型(DOM),你只需知道这些就够了!
  12. Tiny4412之按键驱动
  13. GitLab 社区版 11.0.2用户管理教程
  14. 点评cat系列-应用集成
  15. Unity3D第一战:软件安装与代码调试
  16. Statistical Artifact (error)
  17. 携程的配置中心(阿波罗apollo)
  18. [NOIp2015]运输计划 (二分 $+$ 树上差分)
  19. 凡事不求甚解,遇事必定抓瞎——PHP开发Apache服务器配置备忘录
  20. iOS网络NSURLSession使用详解

热门文章

  1. LINUX:alias命令详解
  2. asp.net MVC分页
  3. 4.Nginx的URL重写应用
  4. python 爬去拉钩测试招聘信息
  5. 三十天学不会TCP,UDP/IP网络编程-TraceRoute的哲学
  6. Android studio Error:org.gradle.api.internal.tasks.DefaultTaskInputs$TaskInputUnionFileCollection cannot be cast to
  7. ASP.NET Core学习之一 入门简介
  8. QCanvasItem介绍-QT3
  9. asp.net core 2.0 查缺补漏
  10. 申请的服务器安装tomcat后不能访问的问题