前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >HDUOJ-----Difference Between Primes

HDUOJ-----Difference Between Primes

作者头像
Gxjun
发布2018-03-21 12:50:58
5320
发布2018-03-21 12:50:58
举报
文章被收录于专栏:mlml

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

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

2013 ACM/ICPC Asia Regional Online —— Warmup

Recommend

liuyiding

快速打素数表:

代码:

代码敲上去较为匆忙!,请自己优化......62ms

代码语言:javascript
复制
 1 #include<iostream>
 2 #include<cstdio>
 3 #define maxn 1000000
 4 using namespace std;
 5 int prime[78500];
 6 bool bo[maxn+5];
 7 int prime_table()
 8 {
 9     int i,j,flag=0;
10     memset(bo,0,sizeof bo);
11     bo[0]=bo[1]=1;
12     for(i=2; i*i<=maxn;i++)
13     {
14         if(!bo[i])
15         {
16           for(j=i*i;j<=maxn;j+=i)
17               bo[j]=1;
18         }
19     }
20      for(i=2;i<=maxn;i++)
21         if(!bo[i]) prime[flag++]=i;
22    return flag;
23 }
24 bool isprime(int a)
25 {
26     for(int i=0;prime[i]*prime[i]<=a;i++)
27     {
28         if(a%prime[i]==0)
29             return 0;
30     }
31     return 1;
32 }
33 
34 int main()
35 {
36   int i,t,b,num;
37   num=prime_table();
38   scanf("%d",&t);
39   while(t--)
40   {
41     scanf("%d",&b);
42     if(b>=0)
43     {
44      for(i=0;i<num;i++)
45       {
46       
47         if(isprime(b+prime[i]))
48        {
49            printf("%d %d\n",prime[i]+b,prime[i]);
50            break;
51        }
52      }
53     }
54     else
55     {
56       for(i=0;i<num;i++)
57       {
58       
59         if(isprime(prime[i]-b))
60        {
61            printf("%d %d\n",prime[i],prime[i]-b);
62            break;
63        }
64     }
65     }
66   }
67   return 0;
68 }
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2013-09-09 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Difference Between Primes
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档