Twin Prime Conjecture

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2659    Accepted Submission(s): 906

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 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
求区间素数,注意打表
#include <cstdio>
#include <cstring>
using namespace std;
const int MAXN=;
bool isPrime[MAXN];
int cnt[MAXN];
int main()
{
int n;
memset(isPrime,true,sizeof(isPrime));
for(int i=;i<MAXN;i++)
{
if(isPrime[i])
{
for(int j=i+i;j<MAXN;j+=i)
isPrime[j]=false;
}
}
cnt[]=;
int pre=;
int num=;
for(int i=;i<MAXN;i++)
{
if(isPrime[i])
{
if(i-pre==)
{
num++;
}
pre=i;
}
cnt[i]=num;
}
while(scanf("%d",&n)!=EOF&&n>=)
{
printf("%d\n",cnt[n]);
}
return ;
}

2011年浙大:Twin Prime Conjecture的更多相关文章

  1. Twin Prime Conjecture(浙大计算机研究生保研复试上机考试-2011年)

    Twin Prime Conjecture                                            Time Limit: 2000/1000 MS (Java/Othe ...

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

    Twin Prime Conjecture Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  3. HDU3792---Twin Prime Conjecture(树状数组)

    Twin Prime Conjecture Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  4. 2011年浙大:Graduate Admission

    题目描述: It is said that in 2011, there are about 100 graduate schools ready to proceed over 40,000 app ...

  5. zoj 4099 Extended Twin Composite Number

    Do you know the twin prime conjecture? Two primes  and  are called twin primes if . The twin prime c ...

  6. 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 ...

  7. HDU_3792_(素数筛+树状数组)

    Twin Prime Conjecture Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  8. 2019/11/02 TZOJ

    1001 ShaoLin http://www.tzcoder.cn/acmhome/problemdetail.do?&method=showdetail&id=6003 标记一下i ...

  9. UVA 10395 素数筛

    Twin Primes Twin primes are pairs of primes of the form (p; p + 2). The term \twin prime" was c ...

随机推荐

  1. leetCode 47.Permutations II (排列组合II) 解题思路和方法

    Permutations II  Given a collection of numbers that might contain duplicates, return all possible un ...

  2. go web的简单服务器

    1)简单web服务器: package main import ( "fmt" "net/http" ) func sayHelloName(w http.Re ...

  3. 实模式切换到保护模式,为什么要开启A20地址线(系统升级产生的兼容性问题)

    [-1]写在前面: 以下部分内容总结于 http://blog.csdn.net/ruyanhai/article/details/7181842 complementary: 兼容性是指运行在前期C ...

  4. 嵌入式c语言笔试

    1 读程序段,回答问题int main(int argc,char *argv[]){int c=9,d=0;c=c++%5;d=c;printf("d=%d\n",d);retu ...

  5. 【Android】第三方库使用的问题集

    Google/百度地图Key的获取 百度地图UnsatisfiedLinkError错误 async-http-client中的FATAL EXCEPTION Google/百度地图Key的获取 无论 ...

  6. JS中try.. catch..的用法

    try 测试代码块的错误. catch 语句处理错误. throw 创建并跑出错误. try { //在这里运行代码 抛出错误 } catch(err) { //在这里处理错误 } 下面是一个实例: ...

  7. Sqlite 设置外键级联更新

    Sqlite 设置外键级联更新 选择好外键表和列以后,勾选更新事件,更新方式设置为CASCADE,即可在外键更新时自动更新

  8. SQL中的四种连接方式

    转自:http://www.cnblogs.com/afirefly/archive/2010/10/08/1845906.html 联接条件可在FROM或WHERE子句中指定,建议在FROM子句中指 ...

  9. 【WinForm】创建自定义控件(转)

    转自:http://www.cnblogs.com/bomo/archive/2012/12/09/2810559.html 虽然VS为我们提供了很多控件可以使用,但有时候这些控件仍然不能满足我们的要 ...

  10. ios导航栏问题

    http://m.blog.csdn.net/article/details?id=47395605