Twin Prime Conjecture(浙大计算机研究生保研复试上机考试-2011年)
Twin Prime Conjecture
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1898 Accepted Submission(s): 592
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.
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.
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.
5
20
-2
1
4
哈哈。开玩笑。。。。。
#include <stdio.h>
#include <string.h> const int MAX = 100000 + 10;
bool prime[MAX];
int r[MAX]; void IsPrime()
{
memset(prime,0,sizeof(prime));
prime[0] = prime[1] = 1;
for(int i = 2;i < MAX;i++)
for(int j = 2;i*j < MAX;j++)
prime[j*i] = 1;
} void sove()
{
IsPrime();
r[0] = r[1] = r[2] = 0;
for(int i = 3;i < MAX;i++)
{
if(!prime[i-2]&&!prime[i])
r[i] = r[i-1] + 1;
else
r[i] = r[i-1];
}
}
int main()
{
int n;
sove();
while(scanf("%d",&n),n>-1)
{
printf("%d\n",r[n]);
}
return 0;
}
#include <iostream>
#define lowbit(t)(-t&t)
using namespace std ; const int MAX = 100001;
int prime[MAX],tree[MAX],num[MAX];
void update(int x,int val)
{
for(int i = x;i < MAX;i += lowbit(i))
tree[i] += val;
}
int GetSum(int x)
{
int sum=0;
for(int i = x;i > 0;i -= lowbit(i))
sum += tree[i];
return sum;
}
int main()
{
int cnt=0;
for(int i=2;i<MAX;i++) if(!prime[i])
{
num[cnt++]=i;
for(int j=i;i<318&&j*i<MAX;j++)
prime[i*j]=1;
}
for(int i=1;i<cnt;i++)
if(num[i]-num[i-1]==2)
update(num[i],1);
int n;
while(cin>>n,n>=0)
{
if(!n) cout<<"0"<<endl;
else cout<<GetSum(n)<<endl;
}
return 0;
}
Twin Prime Conjecture(浙大计算机研究生保研复试上机考试-2011年)的更多相关文章
- hdu 3792 Twin Prime Conjecture 前缀和+欧拉打表
Twin Prime Conjecture Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- 2011年浙大:Twin Prime Conjecture
Twin Prime Conjecture Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- HDU 1234 (浙大计算机研究生复试上机考试-2005年) 开门人和关门人 (水)
开门人和关门人 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Su ...
- HDU3792---Twin Prime Conjecture(树状数组)
Twin Prime Conjecture Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- 九度OJ 1107 搬水果 -- 哈夫曼树 2011年吉林大学计算机研究生机试真题
题目地址:http://ac.jobdu.com/problem.php?pid=1107 题目描述: 在一个果园里,小明已经将所有的水果打了下来,并按水果的不同种类分成了若干堆,小明决定把所有的水果 ...
- 2017计算机学科夏令营上机考试-C:岛屿面积
总时间限制: 1000ms 内存限制: 65536kB 描述 用一个n*m的二维数组表示地图,1表示陆地,0代表海水,每一格都表示一个1*1的区域.地图中的格子只能横向或者纵向连接(不能对角连接) ...
- 2017计算机学科夏令营上机考试-B编码字符串
B:编码字符串 总时间限制: 1000ms 内存限制: 65536kB 描述 在数据压缩中,一个常用的方法是行程长度编码压缩.对于一个待压缩的字符串,我们可以依次记录每个字符及重复的次数.例如,待 ...
- 2017计算机学科夏令营上机考试-A判决素数个数
A:判决素数个数 总时间限制: 1000ms 内存限制: 65536kB 描述 输入两个整数X和Y,输出两者之间的素数个数(包括X和Y). 输入 两个整数X和Y(1 <= X,Y <= ...
- hdu 3794 Magic Coupon
浙大计算机研究生保研复试上机考试-2011年 贪心: 注意:输入输出用scanf printf 可以加快速度,用cin WA #include<iostream> #include&l ...
随机推荐
- mokoid android open source HAL hacking in a picture
/************************************************************************** * mokoid android HAL hac ...
- POJ 1942 Paths on a Grid
// n*m 的格子 从左下角走到右上角的种数// 相当于从 n+m 的步数中选 m 步往上走// C(n+m,m) #include <iostream> #include <st ...
- 剑指offer—第三章高质量代码(o(1)时间删除链表节点)
题目:给定单向链表的头指针和一个节点指针,定义一个函数在O(1)时间删除该节点,链表节点与函数的定义如下:struct ListNode{int m_nValue;ListNode* m_pValue ...
- uglifyjs使用
1.安装Nodejs:去这里下载并安装. 2.打开cmd,运行npm,node应该会有输出东西 3.如果在window上运行Node.js的npm报Error: ENOENT, stat 'C:\Us ...
- informatica9.5.1资源库为machine in exclusive mode(REP_51821)
错误信息: [PCSF_10007]Cannot connect to repository [Rs_RotKang] because [REP_51821]Repository Service is ...
- 嵌入式 H264中的SPS、PPS提取与作用
使用RTP传输H264的时候,需要用到sdp协议描述,其中有两项:Sequence Parameter Sets (SPS) 和Picture Parameter Set (PPS)需要用到,那么这两 ...
- 【转载】HBase基本概念和hbase shell常用命令用法
1. 简介 HBase是一个分布式的.面向列的开源数据库,源于google的一篇论文<bigtable:一个结构化数据的分布式存储系统>.HBase是Google Bigtable的开源实 ...
- 如何配置Drupal数据库信息?
Drupal的数据库连接信息通过文件settings.php中的变量$databases设置.变量$databases是一个二维的数组,第一维称为key,第二维称为target.使用这种方式可以处理多 ...
- Python自动单元测试框架
原文链接:http://www.ibm.com/developerworks/cn/linux/l-pyunit/ 软件的测试是一件非常乏味的事情,在测试别人编写的软件时尤其如此,程序员通常都只对编写 ...
- [转]linux的du和df命令
转自:http://blog.csdn.net/kmesg/article/details/6570800 今天也有同学问我Linux下查看目录大小的命令,现在也将前阵子学习到du/df两个命令总结一 ...