Twin Prime Conjecture

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

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
 
Source
题意:在n以内的孪生素数的对数;

思路:本以为素数打表+暴力能过,结果数据好像很多,求下前缀和打个表就行;

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define mod 1000000007
#define pi (4*atan(1.0))
const int N=1e5+,M=1e6+,inf=1e9+;
const int MAXN=;
int prime[MAXN];
bool vis[MAXN];
int Prime(int n)
{
int cnt=;
memset(vis,,sizeof(vis));
for(int i=;i<n;i++)
{
if(!vis[i])
prime[cnt++]=i;
for(int j=;j<cnt&&i*prime[j]<n;j++)
{
vis[i*prime[j]]=;
if(i%prime[j]==)
break;
}
}
return cnt;
}
int flag[N];
int sum[N];
int main()
{
int ji=Prime(MAXN);
int x,y,z,i,t;
for(i=;i<ji;i++)
{
if(prime[i]-prime[i-]==)
flag[prime[i]]=;
}
for(i=;i<=;i++)
sum[i]=sum[i-]+flag[i];
while(~scanf("%d",&x))
{
if(x<)break;
printf("%d\n",sum[x]);
}
return ;
}

hdu 3792 Twin Prime Conjecture 前缀和+欧拉打表的更多相关文章

  1. 2011年浙大:Twin Prime Conjecture

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

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

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

  3. 『素数 Prime判定和线性欧拉筛法 The sieve of Euler』

    素数(Prime)及判定 定义 素数又称质数,一个大于1的自然数,除了1和它自身外,不能整除其他自然数的数叫做质数,否则称为合数. 1既不是素数也不是合数. 判定 如何判定一个数是否是素数呢?显然,我 ...

  4. HDU 1286:找新朋友(欧拉函数)

    http://acm.hdu.edu.cn/showproblem.php?pid=1286 题意:中文. 思路:求欧拉函数. #include <cstdio> #include < ...

  5. POJ2909_Goldbach's Conjecture(线性欧拉筛)

    Goldbach's Conjecture: For any even number n greater than or equal to 4, there exists at least one p ...

  6. POJ 3126 Prime Path (bfs+欧拉线性素数筛)

    Description The ministers of the cabinet were quite upset by the message from the Chief of Security ...

  7. hdu 3307 Description has only two Sentences (欧拉函数+快速幂)

    Description has only two SentencesTime Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...

  8. HDU 6088 Rikka with Rock-paper-scissors(NTT+欧拉函数)

    题意 \(n\) 局石头剪刀布,设每局的贡献为赢的次数与输的次数之 \(\gcd\) ,求期望贡献乘以 \(3^{2n}\) ,定义若 \(xy=0\) 则,\(\gcd(x,y)=x+y\) 思路 ...

  9. HDU 4549 矩阵快速幂+快速幂+欧拉函数

    M斐波那契数列 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Sub ...

随机推荐

  1. 【BZOJ1529】[POI2005]ska Piggy banks Tarjan

    [BZOJ1529][POI2005]ska Piggy banks Description Byteazar 有 N 个小猪存钱罐. 每个存钱罐只能用钥匙打开或者砸开. Byteazar 已经把每个 ...

  2. HDU 4247 Pinball Game 3D(cdq 分治+树状数组+动态规划)

    Pinball Game 3D Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  3. WebSocket学习记录

    参考资料: Java后端WebSocket的Tomcat实现 基于Java的WebSocket推送 java WebSocket的实现以及Spring WebSocket 利用spring-webso ...

  4. Python开发【项目】:博客后台

    概述 通过自己写的博客后台代码.思路,来与武sir的代码进行一个差异化的比较,记录之间的差距,改善以后写代码的思路 博客后台这个项目,对之前Django学习的各个知识点都有涉及到,非常重要 用户登录验 ...

  5. github push error ---- recursion detected in die handler

    错误提示如下: 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 New Bitmap Image.bmp Coun ...

  6. java:判断二进制数据中第n位是否为1

    可以使用位运算来判断. &是位的与运算符,是指二进制数按位“与”的操作, 逻辑与就是两者都为真的时候才为真,其他真假,假真,假假的运算结果都是假.二进制写法如下 1 & 1 = 1 , ...

  7. python16_day27【crm 内嵌、删除、action】

    一.内嵌 二.删除及关联关联显示 三.action

  8. 爬取乌云上所有人民币和乌云符号的漏洞(python脚本)

    import httplib from HTMLParser import HTMLParser import urlparse import urllib from bs4 import Beaut ...

  9. ng-深度学习-课程笔记-3: Python和向量化(Week2)

    1 向量化( Vectorization ) 在逻辑回归中,以计算z为例,$ z =  w^{T}+b $,你可以用for循环来实现. 但是在python中z可以调用numpy的方法,直接一句$z = ...

  10. springcloud14---zuul

    package com.itmuch.cloud.study; import org.springframework.boot.SpringApplication; import org.spring ...