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
 
题意:对于连续的素数  p(n+1) -pn=2成为一个素数对
          问对于不超过N的素数有多少对素数对 N为负数停止输入
 
题解:预处理 先打一个1~1e5范围内的素数表 标记素数 具体看代码
        然后遍历一遍 i =3~1e5  寻找满足条件的素数对 并记录 存储不超过i的素数对的个数
        然后针对询问输出结果。
 #include<bits/stdc++.h>
#define ll __int64
#define mod 1e9+7
#define PI acos(-1.0)
#define bug(x) printf("%%%%%%%%%%%%%",x);
#define inf 1e8
using namespace std;
int n;
bool visit[];
int prime[];
int ans[];
void fun()
{
memset(visit, true, sizeof(visit));
int num = ;
visit[]=false;
for (int i = ; i <= ; ++i)
{
if (visit[i] == true)
{
num++;
prime[num]=i;
//mp[i]=num;
}
for (int j=;((j<=num)&&(i*prime[j]<= ));++j)
{
visit[i*prime[j]] =false;
if (i%prime[j]==)break; //点睛之笔
}
}
int jishu=;
for(int i=;i<=;i++)
{
if(visit[i]&&visit[i-])
jishu++;
ans[i]=jishu;
}
ans[]=;
ans[]=;
ans[]=;
}
int main()
{
fun();
while(scanf("%d",&n)!=EOF)
{
if(n<)
break;
printf("%d\n",ans[n]);
}
return ;
}
 
 
 

HDU 3792 素数打表的更多相关文章

  1. hdu 5104 素数打表水题

    http://acm.hdu.edu.cn/showproblem.php?pid=5104 找元组数量,满足p1<=p2<=p3且p1+p2+p3=n且都是素数 不用素数打表都能过,数据 ...

  2. HDU 2136 素数打表+求质数因子

    Largest prime factor Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  3. HDU 1397 Goldbach's Conjecture【素数打表】

    题意:给出n,问满足a+b=n且a,b都为素数的有多少对 将素数打表,再枚举 #include<iostream> #include<cstdio> #include<c ...

  4. hdu 3792 Twin Prime Conjecture 前缀和+欧拉打表

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

  5. Codeforces Round #315 (Div. 2C) 568A Primes or Palindromes? 素数打表+暴力

    题目:Click here 题意:π(n)表示不大于n的素数个数,rub(n)表示不大于n的回文数个数,求最大n,满足π(n) ≤ A·rub(n).A=p/q; 分析:由于这个题A是给定范围的,所以 ...

  6. CodeForces 385C Bear and Prime Numbers 素数打表

    第一眼看这道题目的时候觉得可能会很难也看不太懂,但是看了给出的Hint之后思路就十分清晰了 Consider the first sample. Overall, the first sample h ...

  7. LightOJ 1259 Goldbach`s Conjecture 素数打表

    题目大意:求讲一个整数n分解为两个素数的方案数. 题目思路:素数打表,后遍历 1-n/2,寻找方案数,需要注意的是:C/C++中 bool类型占用一个字节,int类型占用4个字节,在素数打表中采用bo ...

  8. hdu 1496 Equations hash表

    hdu 1496 Equations hash表 题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1496 思路: hash表,将原来\(n^{4}\)降 ...

  9. [ACM] POJ 2635 The Embarrassed Cryptographer (同余定理,素数打表)

    The Embarrassed Cryptographer Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 11978   A ...

随机推荐

  1. 题解 CF734A 【Anton and Danik】

    本蒟蒻闲来无事刷刷水题 话说这道题,看楼下的大佬们基本都是用字符 ( char ) 来做的,那么我来介绍一下C++的优势: string ! string,也就是类型串,是C语言没有的,使用十分方便 ...

  2. 修改android studio中的avd sdk路径、avd sdk找不到的解决方案

    要进行Android应用程序的开发,首先就要搭建好Android的开发环境,所需要的工具有如下4个:1.java JDK:2.Android SDK:3.Eclipse:4.ADT 1.java JD ...

  3. final关键字,static关键字

    Final final的意思为最终,不可变.final是个修饰符,它可以用来修饰类,类的成员,以及局部变量.不能修饰构造方法. 注意: 被final修饰的类不能被继承但可以继承别的类 class Yy ...

  4. ArrayList集合例题,商品库存管理(集合)

    创建车库集合,存进车 public class demo1 { String pinpai; String c; int s; } import java.util.ArrayList; class ...

  5. Java基础面试操作题: 获取 1-20 之间的随机数,共计 20 个,要求不能重复 获取 1-20 之间的随机数,共计 10 个,要求不能重

    package com.swift; import java.util.HashSet; import java.util.Random; import java.util.Set; public c ...

  6. Mybatis 插入一条或批量插入 返回带有自增长主键记录

    首先讲一下,  插入一条记录返回主键的 Mybatis 版本要求低点,而批量插入返回带主键的 需要升级到3.3.1版本,3.3.0之前的都不行, <dependency> <grou ...

  7. ngin负载均衡集群(一)

    一.nginx负载均衡集群介绍: 1.反向代理与负载均衡概念简介严格地说, nginx仅仅是作为 Nginx Proxy反向代理使用的,因为这个反向代理功能表现的效果是负载均衡集群的效果,所以本文称之 ...

  8. 【解决】ERROR in xxx.js from UglifyJs

    当我们运行打包脚本npm run build或者打包iosweexpack build ios有可能会遇到以下报错 ERROR in index.js from UglifyJs ![](https: ...

  9. js中如何把RGB颜色转换为16进制颜色

    将RGB颜色值转换为16进制颜色值,主要是将 R.G.B 值分别转换为对应的十六进制值,填入 #RRGGBB 中. 推荐在线颜色转换工具:http://www.ecjson.com/rgbhex/ 例 ...

  10. 谭浩强C第四版(p141)16.输出以下图案

    运行结果: * *** ***** ******* ***** *** * Press any key to continue #include<stdio.h> int main() { ...