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
相关文章推荐
- hduoj 4715 Difference Between Primes 2013 ACM/ICPC Asia Regional Online —— Warmup
- hdu 4715 Difference Between Primes【筛法快速求素数表+思维】
- Hdu 4715 Difference Between Primes (2013网络赛 warm up) & LightOJ 1259 Goldbach`s Conjecture(素数和差)
- HDU 4715 Difference Between Primes (筛法求素数)
- HDU 4715 Difference Between Primes 半个打表题。。
- HDU 4715 Difference Between Primes
- Difference Between Primes----素数筛选
- hdu 4715 Difference Between Primes
- hdu 4715 Difference Between Primes
- HDU 4715 Difference Between Primes(数学啊)
- hdu 4715 Difference Between Primes
- 2013杭电warm_up1 1010 Difference Between Primes
- HDU 4715 Difference Between Primes (素数表+二分)
- hdu4715 Difference Between Primes
- 2013杭电warm_up1 1010 Difference Between Primes
- HDU 4715 Difference Between Primes
- hdu 4715 Difference Between Primes 筛法
- hdu(4715) Difference Between Primes
- 2013杭电warm_up1 1010 Difference Between Primes
- hdu4715 Difference Between Primes