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. 请实现一个函数用来匹配包括'.'和'*'的正则表达式。模式中的字符'.'表示任意一个字符,而'*'表示它前面的字符可以出现任意次(包含0次)。 在本题中,匹配是指字符串的所有字符匹配整个模式。例如,字符串"aaa"与模式"a.a"和"ab*ac*a"匹配,但是与"aa.a"和"ab*a"均不匹配

    // test20.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<iostream> #include< ...

  2. 如何利用hibernate3解决数据库丢失更新问题?

    首先我们要明白什么叫丢失更新. 比如数据库有一个person表,里面有一条这样的数据 "5 zhangsan shenzhen"; 现在有两个事务A.B同时查找了这一条记录: A事 ...

  3. 关于海康视频采集卡的简介---基于pci的插潮采集卡

    vga 640x480 qvga vga的1/4,宽高分别是vga的一半 (1)采集类型 海康威视 DS-2CE16A2P-IT3P 700TVL 1/3" DIS ICR 红外防水筒型摄像 ...

  4. 【题解】Counting D-sets(容斥+欧拉定理)

    [题解]Counting D-sets(容斥+欧拉定理) 没时间写先咕咕咕. vjCodeChef - CNTDSETS 就是容斥,只是难了一二三四五\(\dots \inf\)点 题目大意: 给定你 ...

  5. 对A轮的追逐变得越加狂热,当前距离互联网泡沫到底有多近?

    编者注:本文来自TOMASZ TUNGUZ,中文版由天地会珠海分舵进行编译. 依据NVCA披露的最新数据,在2015年第二季度.VC总共进行了167亿美元的投资,大约是2000年互联网泡沫时候同期的6 ...

  6. 3款Linux网络监视工具

    1 iftop: 如果你想看到现在你的带宽到底是哪些应用在使用,并且各个应用占据了多少带宽的时候,可以用iftop显示出来.使用的参数如下: -h                  display t ...

  7. ThreadLocalMap里Entry声明为WeakReference

    Java里,每个线程都有自己的ThreadLocalMap,里边存着自己私有的对象.Map的Entry里,key为ThreadLocal对象,value即为私有对象T.在spring MVC中,常用T ...

  8. Qt — 子窗体操作父窗体中的方法

    父窗体与子窗体各自的代码如下: 1.  父窗体的代码: void FartherWindow::addactions() { SubWindow subwindow(this); // 把父窗体本身t ...

  9. 跳转appStore评分

    跳转到AppStore让用户能够给我们的应用进行评分,有两种方法,一种是跳出应用,跳转到AppStore,进行评分.另一种是在应用内,内置AppStore进行评分. PS:appleID在https: ...

  10. Maven简介(五)——pom.xml

    6       Maven的pom.xml介绍 6.1     简介 pom.xml文件是Maven进行工作的主要配置文件.在这个文件中我们可以配置Maven项目的groupId.artifactId ...