码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 3792 素数打表

时间:2016-07-16 00:56:57      阅读:186      评论:0      收藏:0      [点我收藏+]

标签:

Description

If we define dn as: dn = pn+1-pn, where pi is the i-th prime. It is easy to see that d1 = 1 and dn=even for n>1. Twin Prime Conjecture states that "There are infinite consecutive primes differing by 2".
Now given any positive integer N (< 10^5), you are supposed to count the number of twin primes which are no greater than N.
 

Input

Your program must read test cases from standard input.
The input file consists of several test cases. Each case occupies a line which contains one integer N. The input is finished by a negative N.
 

Output

For each test case, your program must output to standard output. Print in one line the number of twin primes which are no greater than N.
 

Sample Input

1
5
20
-2
 

Sample Output

0
1
4
 
题意:对于连续的素数  p(n+1) -pn=2成为一个素数对
          问对于不超过N的素数有多少对素数对 N为负数停止输入
 
题解:预处理 先打一个1~1e5范围内的素数表 标记素数 具体看代码
        然后遍历一遍 i =3~1e5  寻找满足条件的素数对 并记录 存储不超过i的素数对的个数
        然后针对询问输出结果。
 1 #include<bits/stdc++.h>
 2 #define ll __int64
 3 #define mod 1e9+7
 4 #define PI acos(-1.0)
 5 #define bug(x) printf("%%%%%%%%%%%%%",x);
 6 #define inf 1e8
 7 using namespace std;
 8 int n;
 9 bool visit[100005];
10 int prime[100005];
11 int ans[100005];
12 void fun()
13 {
14     memset(visit, true, sizeof(visit));
15     int num = 0;
16     visit[1]=false;
17     for (int i = 2; i <= 100000; ++i)
18     {
19         if (visit[i] == true)
20         {
21             num++;
22             prime[num]=i;
23             //mp[i]=num;
24         }
25     for (int j=1;((j<=num)&&(i*prime[j]<= 100000));++j)
26         {
27             visit[i*prime[j]] =false;
28             if (i%prime[j]==0)break; //点睛之笔
29         }
30     }
31     int jishu=0;
32     for(int i=3;i<=100000;i++)
33     {
34         if(visit[i]&&visit[i-2])
35             jishu++;
36         ans[i]=jishu;
37     }
38     ans[0]=0;
39     ans[1]=0;
40     ans[2]=0;
41 }
42 int main()
43 {
44     fun();
45     while(scanf("%d",&n)!=EOF)
46     {
47         if(n<0)
48             break;
49         printf("%d\n",ans[n]);
50     }
51     return 0;
52 }

 

 
 
 

HDU 3792 素数打表

标签:

原文地址:http://www.cnblogs.com/hsd-/p/5675017.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!