Difference Between Primes

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 528    Accepted Submission(s): 150
Problem Description
All you know Goldbach conjecture.That is to say, Every even integer greater than 2 can be expressed as the sum of two primes. Today, skywind present a new conjecture: every even integer can be expressed as the difference of two primes. To validate this conjecture, you are asked to write a program.
 
Input
The first line of input is a number nidentified the count of test cases(n<10^5). There is a even number xat the next nlines. The absolute value of xis not greater than 10^6.

 
Output
For each number xtested, outputstwo primes aand bat one line separatedwith one space where a-b=x. If more than one group can meet it, output the minimum group. If no primes can satisfy it, output 'FAIL'.

 
Sample Input
3
6
10
20
 
Sample Output
11 5
13 3
23 3
 
Source


思路:
打个素数表,然后枚举b,判断a就够了。
ps:汗,开始以为xat为正数,一直WA,原来题目说的是绝对值。

代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#define maxn 10000005
using namespace std; int n,m,cxx,ans;
bool vis[maxn];
int prime[664580]; void sieve(int nn)
{
int i,j,mm;
mm=int(sqrt(nn+0.5));
memset(vis,0,sizeof(vis));
for(i=2;i<=mm;i++)
{
if(!vis[i])
{
for(j=i*i;j<=nn;j+=i)
{
vis[j]=1;
}
}
}
}
int get_prime(int nn)
{
int i,c=0;
sieve(nn);
for(i=2;i<=nn;i++)
{
if(!vis[i]) prime[c++]=i;
}
return c;
}
bool isprime(int x)
{
int i,j,t;
t=sqrt(x+0.5);
for(i=2;i<=t;i++)
{
if(x%i==0) return false ;
}
return true ;
}
int main()
{
int i,j,t,a,b,flag,sgn;
cxx=get_prime(10000000); // 664579
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
flag=0;
if(n==0)
{
printf("2 2\n");
continue ;
}
if(n>0) sgn=1;
else sgn=0,n=-n;
for(i=0;i<cxx;i++)
{
b=prime[i];
a=b+n;
if(isprime(a))
{
flag=1;
if(sgn) printf("%d %d\n",a,b);
else printf("%d %d\n",b,a);
break ;
}
}
if(!flag) printf("FAIL\n");
}
return 0;
}



 

hdu 4715 Difference Between Primes (打表 枚举)的更多相关文章

  1. HDU 4715 Difference Between Primes (打表)

    Difference Between Primes Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...

  2. hdu 4715 Difference Between Primes

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4715 Difference Between Primes Description All you kn ...

  3. hdu 4715 Difference Between Primes(素数筛选+树状数组哈希剪枝)

    http://acm.hdu.edu.cn/showproblem.php?pid=4715 [code]: #include <iostream> #include <cstdio ...

  4. hdu 4715 Difference Between Primes 2013年ICPC热身赛A题 素数水题

    题意:给出一个偶数(不论正负),求出两个素数a,b,能够满足 a-b=x,素数在1e6以内. 只要用筛选法打出素数表,枚举查询下就行了. 我用set储存素数,然后遍历set里面的元素,查询+x后是否还 ...

  5. hduoj 4715 Difference Between Primes 2013 ACM/ICPC Asia Regional Online —— Warmup

    http://acm.hdu.edu.cn/showproblem.php?pid=4715 Difference Between Primes Time Limit: 2000/1000 MS (J ...

  6. hdoj 4715 Difference Between Primes 素数筛选+二分查找

    #include <string.h> #include <stdio.h> const int maxn = 1000006; bool vis[1000006]; int ...

  7. HDU 4715:Difference Between Primes

    Difference Between Primes Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Jav ...

  8. HDU 4548 美素数(打表)

    HDU  4548  美素数(打表)解题报告 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=88159#problem/H 题目 ...

  9. HDU 5487 Difference of Languages(BFS)

    HDU 5487 Difference of Languages 这题从昨天下午2点开始做,到现在才AC了.感觉就是好多题都能想出来,就是写完后debug很长时间,才能AC,是不熟练的原因吗?但愿孰能 ...

随机推荐

  1. 多个线程怎样操作同一个epoll fd

    自己曾经做一个接口server时候,这样的场景下我的设计是多个线程操作同一个epoll fd.彼时,我的理由是epoll的系列函数是线程安全的. 当然有人不理解为什么会有多个线程操作同一个epoll ...

  2. 再写KMP算法

    #include<iostream> #include<string> using namespace std; void getNext(char const*T,int l ...

  3. java--实例成员 & 静态成员

    class run{ static String str1 = "静态变量"; String str2 = "非静态变量"; public static voi ...

  4. android 4.0后不允许屏蔽掉home键

    屏蔽home键的方法 // 屏蔽掉Home键 public void onAttachedToWindow() { this.getWindow().setType(WindowManager.Lay ...

  5. codeforces 316F3 Suns and Rays

    题目在此 找出中有多少个太阳以及每个太阳的散发线段. 算法 原图: 将图"缩小",如果一个白点的四周有黑点,那么把这个白点变成黑点: 将图"放大",即上述&qu ...

  6. js导出table到excel,同时兼容FF和IE

    前台调用(第一个参数是table的id): <input value="导出" type="button" /> function toExcel( ...

  7. C# - 委托_ 匿名方法

    代码: using System; using System.Collections.Generic; using System.Linq; using System.Text; using Syst ...

  8. Javascript 调试利器 Firebug使用详解

    Javascript 调试利器 Firebug使用详解 有时候,为了更清楚方便的查看输出信息,我们可能需要将一些调试信息进行分组输出,那么可以使用console.group来对信息进行分组,在组信息输 ...

  9. IOS开发之UIView的基本使用

    一.视图 1. iphone手机上的窗口就是UIWindow类的一个实例(1个手机应用只有一个UIWindow). 2.UIView类用于实现视图. UIView提供了方法来添加和删除子视图.一个视图 ...

  10. 1数组的join方法

    function log(e) { console.log(e) } 有时候写console.log太长了,所以会自己写个这样的函数省去写console的步骤. 数组的join方法可以把一个数组按照j ...