Twin Prime Conjecture

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3280    Accepted Submission(s): 1162

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的孪生素数对有多少个。
 
素数筛+树状数组
 
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
#define N 100005 int s[N];
int prime[N],cntp;
bool isNot[N]; void getPrime()
{
for(int i=;i<=N;i++)
{
if(isNot[i]==)
{
prime[cntp++]=i;
for(int j=;i*j<=N;j++)
isNot[i*j]=;
}
}
}
int lowbit(int x)
{
return x&(-x);
} void add(int x,int num)
{
while(x<=N)
{
s[x]+=num;
x+=lowbit(x);
}
} int sum(int x)
{
int res=;
while(x>)
{
res+=s[x];
x-=lowbit(x);
}
return res;
} int main()
{
cntp=;
getPrime();
for(int i=;i<cntp;i++)
if(prime[i]-prime[i-]==)
add(prime[i],);
int n;
while(scanf("%d",&n)!=EOF&&n>)
{
printf("%d\n",sum(n));
}
return ;
}

HDU_3792_(素数筛+树状数组)的更多相关文章

  1. 哈理工赛 H-小乐乐学数学 /// 筛法得素数表+树状数组

    题目大意: 给定n个数 m个询问 询问l r区间内的孤独数的个数 孤独数的定义为在该区间内与其他所有数互质的数 看注释 #include <bits/stdc++.h> using nam ...

  2. hdu 4715 Difference Between Primes(素数筛选+树状数组哈希剪枝)

    http://acm.hdu.edu.cn/showproblem.php?pid=4715 [code]: #include <iostream> #include <cstdio ...

  3. Gym - 102082G What Goes Up Must Come Down (树状数组+贪心)

    题意:有一个长度为n的序列,你每次可以选择两个相邻的元素交换,求把这个序列排成单峰序列的最少交换次数. 方法一:将元素按数值从大到小排序(保存原来的位置),把最大的插在中间,剩下的依次往两边放,依次考 ...

  4. uva11610 树状数组+素数打表求因子,好题!

    /* uva11610 树状数组+素数打表+离散化 打出的素数范围在2-1000000之间,不超过六位数,然后按照格式翻转成七位数 */ #include<bits/stdc++.h> u ...

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

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

  6. Codeforces986E Prince's Problem 【虚树】【可持久化线段树】【树状数组】

    我很喜欢这道题. 题目大意: 给出一棵带点权树.对每个询问$ u,v,x $,求$\prod_{i \in P(u,v)}gcd(ai,x)$.其中$ P(u,v) $表示$ u $到$ v $的路径 ...

  7. BZOJ 3529: [Sdoi2014]数表 [莫比乌斯反演 树状数组]

    3529: [Sdoi2014]数表 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 1399  Solved: 694[Submit][Status] ...

  8. CF459D Pashmak and Parmida's problem (树状数组)

    Codeforces Round #261 (Div. 2)   题意:给出数组A,定义f(l,r,x)为A[]的下标l到r之间,等于x的元素数.i和j符合f(1,i,a[i])>f(j,n,a ...

  9. [bzoj2124]等差子序列(hash+树状数组)

    我又来更博啦     2124: 等差子序列 Time Limit: 3 Sec  Memory Limit: 259 MBSubmit: 941  Solved: 348[Submit][Statu ...

随机推荐

  1. Latex 1: 解决latex中遇到一个常见错误:"Improper alphabetic constant."

    1.问题: 本人是在WIN7下用texlive 2016,编辑器用的是WinEdt 10.1 ,运行如下代码: \documentclass{ctexbook} \begin{document} \t ...

  2. wpa_supplicant - 强有力的终端 wifi 配置工具【转】

    本文转载自:http://rickgray.me/2015/08/03/useful-command-tool-for-wifi-connection.html 最近网购了一套Raspberry-Pi ...

  3. bat文件中调用传参的问题

    https://blogs.msdn.microsoft.com/twistylittlepassagesallalike/2011/04/23/everyone-quotes-command-lin ...

  4. codeforces 688A A. Opponents(水题)

    题目链接: A. Opponents time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  5. win10下的jdk1.8安装

    1 下载jdk 下一步就好 默认C盘最好 不要又中文  http://pan.baidu.com/s/1eSwvFA6(这里面有两个jdk 32位 64位) 看图 CLASSPATH: .;%JAVA ...

  6. 【BZOJ3625】【CF438E】小朋友和二叉树

    题目 传送门 思路&做法 我们可以用\(v_i\)表示\(i\)在\(c\)中出现了几次, 用\(f_i\)表示权值为\(i\)的神犇树的总数, 于是 \[ f_x = \sum_{i = 0 ...

  7. poj 2699 The Maximum Number of Strong Kings【最大流+枚举】

    因为n很小所以从大到小枚举答案.(从小到大先排个序,因为显然胜利场次越多越容易成为strong king.然后对于每个枚举出来的ans建图.点分别表示人和比赛.s向所有人连接流量为胜利场次的边,所有比 ...

  8. ppt自动制作器

    讲一篇论文导入进入,自动生成该论文的展示ppt,啧啧啧,想想岂不是很美妙

  9. 进击的Python【第十章】:Python的高级应用(多进程,进程间通信,协程与异步,牛逼的IO多路复用)

    Python的socket高级应用(多进程,协程与异步) 一.多进程multiprocessing multiprocessing is a package that supports spawnin ...

  10. Python生成器实现斐波那契数列

    比如,斐波那契数列:1,1,2,3,5,8,13,21,34.... 用列表生成式写不出来,但是我们可以用函数把它打印出来: def fib(number): n, a, b = 0, 0, 1 wh ...