hdu 4715(Difference Between Primes)

发布时间:2017-1-19 6:11:18 编辑:www.fx114.net 分享查询网我要评论
本篇文章主要介绍了"hdu 4715(Difference Between Primes)",主要涉及到hdu 4715(Difference Between Primes)方面的内容,对于hdu 4715(Difference Between Primes)感兴趣的同学可以参考一下。

题目链接:点击打开链接 题目大意:give you an even , you should output  the difference of 2 primes 题目分析:even though it says if you cannot find the 2 primes you may printf(" fail")                     yet,if you think carefully you will find that there must exist    such primes.--->                     题目总结:明明清晰明了的代码,runtime error了= =                    又是sb 的小错误,什么时候才能改掉啊……                     if(flag=-1)  x=0-x;   有点怪异吧~!!! 应该是if (flag ==-1) x=0-x;                    说多了是泪== #pragma comment(linker, "/STACK:16777216")//为了不re还开了栈开关- - #include<cstdio> #include<cstring> #include<cstdlib> #include<cmath> #include<algorithm> using namespace std; bool is_prime[1000800];//用之前的算法得出1000004之内的数都可以满足猜想,并且最大的ans才是1000151,所以筛选质数自然就可以确定范围;了 void init() { for(int i=0; i<1000800; i++) is_prime[i]=true; for(int i=2; i*i < 1000800;i++) if(is_prime[i]) { for(int j=2*i; j<1000800; j+=i) is_prime[j]=false; } } void solve(int x,int flag) { if(flag==-1) x=0-x; int i=x+2; while(i<1000800) { if(is_prime[i]&&is_prime[i-x]) { if(flag==1) printf("%d %d\n",i,i-x); else printf("%d %d\n",i-x,i); break; } i++; } } int main() { int n,x; init(); scanf("%d",&n); while(n--) { scanf("%d",&x); if (x<0) solve(x,-1); else solve(x,1); } return 0; }

上一篇:速成笔记:Hypertext Transfer Protocol