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 ...
随机推荐
- MapView的用法
一.MapView 1.显示用户的位置点(用蓝色圆点标记) mapView.showsUserLocation = YES; 2.代理方法 1> 当定位到用户的位置就会调用 - (void)ma ...
- cakephp之查询
find public find( string $type 'first' , array $query array() ) Queries the datasource and returns a ...
- uimodalpresentationformsheet resize ios7
CROHomeCRAAddController *temp =[[CROHomeCRAAddControlleralloc] init]; temp.modalTransitionStyle = UI ...
- mysql 错误代码汇总
1005:创建表失败1006:创建数据库失败1007:数据库已存在,创建数据库失败1008:数据库不存在,删除数据库失败1009:不能删除数据库文件导致删除数据库失败1010:不能删除数据目录导致删除 ...
- appdata文件夹有什么用途?C盘appdata可以删除吗?
在内存紧张的时候,我们都会选择删除一些无关紧要的大文件来释放内存,有不少网友发现在系统C盘下有一个appdata文件夹,而且体积挺大的,不知道能不能删除,针对此问题,本文就为大家介绍appdata文件 ...
- ANDROID模拟器访问本地WEB应用
一个早上就是想让android的模拟器可以访问到web的应用程序,但是一直是不可以,弄的不知所措. 在一般的Java Web程序开发中,我们通常使用localhost或者127.0.0.1来访问本机的 ...
- MySQL与Oracle 差异比较之三函数
函数 编号 类别 ORACLE MYSQL 注释 1 数字函数 round(1.23456,4) round(1.23456,4) 一样:ORACLE:select round(1.23456,4) ...
- xampp 提示 This setting can be configured in the file "httpd-xampp.conf".
错误提示如下: New XAMPP security concept: Access to the requested object is only available from the local ...
- 二叉树的基本操作(C)
实现二叉树的创建(先序).递归及非递归的先.中.后序遍历 请按先序遍历输入二叉树元素(每个结点一个字符,空结点为'='): ABD==E==CF==G== 先序递归遍历: A B D E C F G ...
- 插入排序 --- 排序算法 --- 算法 --- java
设数组为a[0…n-1]. 1. 初始时,a[0]自成1个有序区,无序区为a[1..n-1].令i=1 2. 将a[i]并入当前的有序区a[0…i-1]中形成a[0…i]的有序区间 ...