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. 博弈论中的SG函数

    SG函数的定义: g(x) = mex ( sg(y) |y是x的后继结点 ) 其中mex(x)(x是一个自然是集合)函数是x关于自然数集合的补集中的最小值,比如x={0,1,2,4,6} 则mex( ...

  2. 2-6 Opencv模块组织结构

    https://opencv.org/releases.html https://sourceforge.net/projects/opencvlibrary/files/opencv-win/3.4 ...

  3. 07_传智播客iOS视频教程_#import指令

    mac切换中英文输入法默认是Ctrl+空格键. 预处理指令的执行时机是在编译之前.在编译之前执行预处理指令. #import指令是包含文件,将指定的文件的内容在预编译的时候拷贝到写指令的地方. #im ...

  4. Java多线程系列九——Atomic类

    参考资料:https://fangjian0423.github.io/2016/03/16/java-AtomicInteger-analysis/http://www.cnblogs.com/54 ...

  5. Quartz.NET 快速入门

    官网:http://www.quartz-scheduler.net/ API:http://www.quartz-scheduler.net/documentation/index.html 快速入 ...

  6. bzoj 3732: Network【克鲁斯卡尔+树链剖分】

    先做最小生成树,这样就保证了最大值最小 然后随便用个什么东西维护一下最大值,我用的树剖log^2,倍增会更快 #include<iostream> #include<cstdio&g ...

  7. 洛谷 P1606 [USACO07FEB]荷叶塘Lilypad Pond【spfa】

    和bzoj同名题不一样! 起点和水点向花费一个荷花能到的第一个点连一条边权为1的有向边,然后跑计数spfa即可 #include<iostream> #include<cstdio& ...

  8. [SDOI2019] 移动金币

    分析 阶梯NIM模型:共有m+1堆石子,石子总数不超过n-m,求必胜的,即奇数堆石子数目异或和非零的局面数.补集转化,答案C(n,m)-奇数堆石子数目异或和位0的局面数. 可以想到按位dp,设f[i, ...

  9. bzoj2581 [USACO 2012 Jan Gold] Cow Run【And-Or Tree】

    传送门1:http://www.usaco.org/index.php?page=viewproblem2&cpid=110 传送门2:http://www.lydsy.com/JudgeOn ...

  10. 贪心 UVALive 6834 Shopping

    题目传送门 /* 题意:有n个商店排成一条直线,有一些商店有先后顺序,问从0出发走到n+1最少的步数 贪心:对于区间被覆盖的点只进行一次计算,还有那些要往回走的区间步数*2,再加上原来最少要走n+1步 ...