您的位置:首页 > Web前端

HDUOJ-----Difference Between Primes

2013-09-09 21:56 225 查看

Difference Between Primes

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 832 Accepted Submission(s): 267

[align=left]Problem Description[/align]
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.

[align=left]Input[/align]
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.

[align=left]Output[/align]
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'.

[align=left]Sample Input[/align]

3 6 10 20

[align=left]Sample Output[/align]

11 5 13 3 23 3

[align=left]Source[/align]
2013 ACM/ICPC Asia Regional Online —— Warmup

[align=left]Recommend[/align]
liuyiding

快速打素数表:
代码:
代码敲上去较为匆忙!,请自己优化......62ms

#include<iostream>
#include<cstdio>
#define maxn 1000000
using namespace std;
int prime[78500];
bool bo[maxn+5];
int prime_table()
{
int i,j,flag=0;
memset(bo,0,sizeof bo);
bo[0]=bo[1]=1;
for(i=2; i*i<=maxn;i++)
{
if(!bo[i])
{
for(j=i*i;j<=maxn;j+=i)
bo[j]=1;
}
}
for(i=2;i<=maxn;i++)
if(!bo[i]) prime[flag++]=i;
return flag;
}
bool isprime(int a)
{
for(int i=0;prime[i]*prime[i]<=a;i++)
{
if(a%prime[i]==0)
return 0;
}
return 1;
}

int main()
{
int i,t,b,num;
num=prime_table();
scanf("%d",&t);
while(t--)
{
scanf("%d",&b);
if(b>=0)
{
for(i=0;i<num;i++)
{

if(isprime(b+prime[i]))
{
printf("%d %d\n",prime[i]+b,prime[i]);
break;
}
}
}
else
{
for(i=0;i<num;i++)
{

if(isprime(prime[i]-b))
{
printf("%d %d\n",prime[i],prime[i]-b);
break;
}
}
}
}
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: