Twin Prime Conjecture

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 2950    Accepted Submission(s): 1016
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
 
题目大意:
题目的意思是,给定一个数字n。求小于n的孪生素数有多少对。孪生素数的概念就是说,相邻的两个素数之间差值为2。例如:5和7,17和19,这些都是。

解题思路:
先用筛法打了一个素数表,在素数表的基础上打出来一个直接保存结果的数组c[](树状数组直接处理的数组)。这样就可以直接用树状数组来解决了。基础的BIT应用。
如果不懂树状数组的话,可以去看这篇博客:搞懂树状数组

源代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<stack>
#include<queue>
#include<vector>
#include<deque>
#include<map>
#include<set>
#include<algorithm>
#include<string>
#include<iomanip>
#include<cstdlib>
#include<cmath>
#include<sstream>
#include<ctime>
using namespace std; typedef long long ll;
#define MAXN 100005
int c[MAXN];//c[]从下标为1的地方开始 int lowbit(int x)
{
return x&(-x);
} void add(int x, int num)//x代表位置,num代表增量
{
while(x<=MAXN)
{
c[x]+=num;
x+=lowbit(x);
}
} int sum(int x)//代表求解1~x的和(x是下标)
{
int res = 0;
while(x>0)
{
res+=c[x];
x-=lowbit(x);
}
return res;
} int primer[MAXN];
int number[MAXN]; void init()
{
memset(number,0,sizeof(number));
int i,j;
int cnt=0;
for(i=2;i<=100000;i++)
{
if(number[i]==0)
{
cnt++;
primer[cnt]=i;
for(j=i*2;j<=100000;j+=i)
{
number[j]=1;
}
}
}//素数表OK
for(i=2;i<=cnt;i++)
{
if(primer[i]-primer[i-1]==2)
{
add(primer[i],1);//发现一对增加一个
}
}
}
int main()
{
int n;
init();
while(scanf("%d",&n)!=EOF&&n>0)
{
printf("%d\n",sum(n));
}
return 0;
}




HDU3792---Twin Prime Conjecture(树状数组)的更多相关文章

  1. UVA 11610 Reverse Prime (数论+树状数组+二分,难题)

    参考链接http://blog.csdn.net/acm_cxlove/article/details/8264290http://blog.csdn.net/w00w12l/article/deta ...

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

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

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

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

  4. HDU 4947 GCD Array 容斥原理+树状数组

    GCD Array Time Limit: 11000/5500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total ...

  5. HDU 4746 莫比乌斯反演+离线查询+树状数组

    题目大意: 一个数字组成一堆素因子的乘积,如果一个数字的素因子个数(同样的素因子也要多次计数)小于等于P,那么就称这个数是P的幸运数 多次询问1<=x<=n,1<=y<=m,P ...

  6. bzoj3529(莫比乌斯反演+离线+树状数组)

    在你以为理解mobus的时候,苦苦想通过化简公式来降低复杂度时,这题又打了我一巴掌. 看来我并没有理解到acmicpc比赛的宗旨啊. 这么多次查询可以考虑离线操作,使用树状数组单点更新. /***** ...

  7. Codeforce385C 树状数组+素因子分解

    题目大意: 给多个区间的询问,在询问区间内每一个出现的素数去计算所有数中有多少个数能被这个素数整除 然后将所有素数得到的对应值求和 这里因为初始给定的数不超过10000000,最多670000不到的素 ...

  8. HDU 4777 Rabbit Kingdom 树状数组

    分析:找到每一个点的左边离他最近的不互质数,记录下标(L数组),右边一样如此(R数组),预处理 这个过程需要分解质因数O(n*sqrt(n)) 然后离线,按照区间右端点排序 然后扫一遍,对于当前拍好顺 ...

  9. 【BZOJ3529】【莫比乌斯反演 + 树状数组】[Sdoi2014]数表

    Description 有一张N×m的数表,其第i行第j列(1 < =i < =礼,1 < =j < =m)的数值为 能同时整除i和j的所有自然数之和.给定a,计算数表中不大于 ...

随机推荐

  1. LeetCode 292. Nim Game (取物游戏)

    You are playing the following Nim Game with your friend: There is a heap of stones on the table, eac ...

  2. Ubuntu远程登陆、SSH图形界面、WOL远程唤醒

    本文为作者原创,转载请注明出处(http://www.cnblogs.com/mar-q/)by 负赑屃 实现目标:通过路由器配置路由路径,将拨号获取的公网IP地址指向局域网Ubuntu服务器.家里有 ...

  3. Vue-cli安装教程

    第一步:安装vue-cli npm install vue-cli -g -g :代表全局安装.如果你安装时报错,一般是网络问题,你可以尝试用cnpm来进行安装. 检测是否安装成功:可以用vue -V ...

  4. linux GCC 编译多个.c/.h文件

    基本认识: #include <xxx>:首先去系统目录中找头文件,如果没有在到当前目录下找.像标准的头文件 stdio.h.stdlib.h等用这个方法. #include " ...

  5. 0_Simple__clock + 0_Simple__clock_nvrtc

    使用 clock() 函数在CUDA核函数内部进行计时,将核函数封装为PTX并在另外的代码中读取和使用. ▶ 源代码:文件内建核函数计时. #include <stdio.h> #incl ...

  6. 开发一个基于 Android系统车载智能APP

    很久之前就想做一个车载相关的app.需要实现如下功能: (1)每0.2秒更新一次当前车辆的最新速度值. (2)可控制性记录行驶里程. (3)不连接网络情况下获取当前车辆位置.如(北京市X区X路X号) ...

  7. Spring AOP高级——源码实现(1)动态代理技术

    在正式进入Spring AOP的源码实现前,我们需要准备一定的基础也就是面向切面编程的核心——动态代理. 动态代理实际上也是一种结构型的设计模式,JDK中已经为我们准备好了这种设计模式,不过这种JDK ...

  8. python jason,pickle

    参考官网 https://docs.python.org/3/library/json.html https://docs.python.org/3/library/pickle.html 了解这两个 ...

  9. Get started with Google Analytics

    What is Google Analytics Google Analytics is a Google official analytics tool that is primarily used ...

  10. 根据自己的博客数据统计国内IT人群

    装上百度统计有一段时间了,今天突然找出报表看看,发现一个很有意思的事情.访问来源TOP5依次是:北京,上海,深圳,杭州,广州 虽然大部分文章都是当时特别白的时候记录下来的遇到过的问题,但受众确实应该只 ...