HDU 3792 素数打表

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 }
时间: 2024-10-10 09:02:08

HDU 3792 素数打表的相关文章

hdu 4715 素数打表

先利用筛法完成素数打表 再从小到大判断即可 #include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<vector> using namespace std; const int Max = 1e6 + 50; int n; int isPrime[Max]; int tblPrime[Max];

HDU 1397 Goldbach&#39;s Conjecture(素数打表)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1397 Problem Description Goldbach's Conjecture: For any even number n greater than or equal to 4, there exists at least one pair of prime numbers p1 and p2 such that n = p1 + p2. This conjecture has not

HDU 1397 Goldbach&#39;s Conjecture【素数打表】

题意:给出n,问满足a+b=n且a,b都为素数的有多少对 将素数打表,再枚举 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include <cmath> 5 #include<stack> 6 #include<vector> 7 #include<map> 8 #include<algorithm> 9 #define mod=

hdu 3792 Twin Prime Conjecture 前缀和+欧拉打表

Twin Prime Conjecture Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem 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

hdu 2161 Primes 素数打表

在kuangbin带你飞专题看到的,水了一发,但是wa了一次,T了一次,竟然连素数打表都快不会写了. 而且连求素数时候只需到根号n就可以都忘了,假设有因子m大于√n,那么n/m一定小于√n,所以它在√n前面已经被选出来了. 代码: #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<cmath> #include<vector>

[ACM] POJ 2635 The Embarrassed Cryptographer (同余定理,素数打表)

The Embarrassed Cryptographer Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 11978   Accepted: 3194 Description The young and very promising cryptographer Odd Even has implemented the security module of a large system with thousands of

POJ 1365(质因数分级+素数打表)

Prime Land Time Limit: 1000ms                                Memory Limit: 10000KB Everybody in the Prime Land is using a prime base number system. In this system, each positive integer x is represented as follows: Let {pi}i=0,1,2,... denote the incr

hdu 1012 素数判定

这道题~以前判定prime是一个个去试着整除再去存储,上次弄过欧拉函数那题目之后就知道了,这样会更快捷: 1 prime[0] = prime[1] = 1; 2 for(int i = 2; i <maxn; i++) 3 { 4 if(!prime[i]) 5 { 6 for(int j = i * 2; j < maxn; j += i) 7 prime[j] = 1; 8 } 9 } 以下是AC代码~~~水水题~ 1 #include<iostream> 2 #includ

hdu 2012 素数判定 Miller_Rabbin

素数判定 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 71785    Accepted Submission(s): 24969 Problem Description 对于表达式n^2+n+41,当n在(x,y)范围内取整数值时(包括x,y)(-39<=x<y<=50),判定该表达式的值是否都为素数. Input 输入数据