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 ...
随机推荐
- HDU1010 Tempter of the Bone
解题思路:相当经典的一题,回溯,具体细节处理见代码. #include<cstdio> #include<cstring> #include<algorithm> ...
- Be quiet
Be quiet */--> UP | HOME Be quiet Table of Contents 1 Be quiet 1 Be quiet 最近心情有点不太好,各方面原因.主要是25岁是 ...
- 【转】关于Python脚本开头两行的:#!/usr/bin/python和# -*- coding: utf-8 -*-的作用 – 指定文件编码类型
原文网址:http://www.crifan.com/python_head_meaning_for_usr_bin_python_coding_utf-8/ #!/usr/bin/python 是用 ...
- gcc/交叉编译
一.gcc编译的情况: 1.linux gcc包含的c/c++编译器,gcc和cc是一样的,c++和g++是一样的,一般c程序就用gcc编译,c++程序就用g++编译. 2.linux gcc的应用: ...
- org.hibernate.MappingException: duplicate import异常
在开发hibernate时,一起多谢ORM类和映射文件时,报出:org.hibernate.MappingException: duplicate import com.XXX异常 解决方案: 检查你 ...
- MySQL与Oracle 差异比较之三函数
函数 编号 类别 ORACLE MYSQL 注释 1 数字函数 round(1.23456,4) round(1.23456,4) 一样:ORACLE:select round(1.23456,4) ...
- 关于“怎么比较两个SQL查询语句产生的结果集是否相同”搜集到的一些资料
一. 如果你这两个结果集都没有close,可以这样: rs.last(); rs2.last(); if (rs.getRow() != rs2.getRow()) { //记录数不等,即结果不等 } ...
- js事件处理相关-实现一个div的拖拽
最终代码: <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" c ...
- C#的Timer
PowerCoder 原文 C#的Timer 再C#里现在有3个Timer类: System.Windows.Forms.Timer System.Threading.Timer System.Tim ...
- mysql 用户名密码登陆不上
问题1:刚安装完mysql,设置了用户名密码root,登陆OK的,后来再连怎么也连不上了 操作步骤: 输入:mysql -uroot -proot 提示:ERROR 1045 (28000): Acc ...