您的位置:首页 > Web前端

hdu 4715 Difference Between Primes

2013-09-08 22:36 471 查看

Difference Between Primes

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 496 Accepted Submission(s): 136



[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

思路:用筛法将素数打表,然后二分查找,学会low_bound()函数,挺好用的

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <queue>
#include <stack>
#include <vector>
#include <cmath>
using namespace std;
const int maxn=500000;
bool isprime[maxn];
int  prime[maxn],nprime;
void pre_solve()
{
nprime=0;
isprime[1]=0;
for(int i=2;i<maxn;i++) isprime[i]=true;
for(long long i=2;i<maxn;i++)
{
if(isprime[i])
{
prime[++nprime]=i;
for(long long j=i*i;j<maxn;j+=i)
isprime[j]=false;
}
}
}
int main()
{
//freopen("in.txt","r",stdin);
pre_solve();
/* for(int i=1;i<=1;i++)
cout<<prime[i]<<" ";*/
int n;
scanf("%d",&n);
while(n--)
{
int x;
bool isfind=false;
scanf("%d",&x);
for(int i=1;i<=nprime;i++)
{
int t=lower_bound(prime+1,prime+1+nprime,prime[i]+x)-prime;
if(prime[t]==prime[i]+x)
{
printf("%d %d\n",prime[t],prime[i]);
isfind=true;
break;
}
}
if(!isfind) printf("FALL\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: