Twin Primes

Twin primes are pairs of primes of the form (p; p + 2). The term \twin prime" was coined by Paul
Stckel (1892-1919). The rst few twin primes are (3, 5), (5, 7), (11, 13), (17, 19), (29, 31), (41, 43).
In this problem you are asked to nd out the S-th twin prime pair where S is an integer that will be
given in the input.
Input
The input will contain less than 10001 lines of input. Each line contains an integers S (1 S 100000),
which is the serial number of a twin prime pair. Input le is terminated by end of le.
Output
For each line of input you will have to produce one line of output which contains the S-th twin prime
pair. The pair is printed in the form (p1,<space>p2). Here <space> means the space character (ASCII
32) . You can safely assume that the primes in the 100000-th twin prime pair are less than 20000000.
Sample Input
1
2
3
4
Sample Output
(3, 5)
(5, 7)
(11, 13)
(17, 19)Twin primes are pairs of primes of the form (p; p + 2). The term \twin prime" was coined by Paul
Stckel (1892-1919). The rst few twin primes are (3, 5), (5, 7), (11, 13), (17, 19), (29, 31), (41, 43).
In this problem you are asked to nd out the S-th twin prime pair where S is an integer that will be
given in the input.
Input
The input will contain less than 10001 lines of input. Each line contains an integers S (1 S 100000),
which is the serial number of a twin prime pair. Input le is terminated by end of le.
Output
For each line of input you will have to produce one line of output which contains the S-th twin prime
pair. The pair is printed in the form (p1,<space>p2). Here <space> means the space character (ASCII
32) . You can safely assume that the primes in the 100000-th twin prime pair are less than 20000000.
Sample Input
1
2
3
4
Sample Output
(3, 5)
(5, 7)
(11, 13)
(17, 19)

题意:

定义双素数(p,p+2),p,p+2都为素数。

先输入若干个n,输出第n对双素数。

分析:

时间限制为3000ms,数据量为2e7,可以直接暴力求解。

首先用欧拉筛筛出所有素数,然后暴力枚举所有素数,判断是否是双素数即可。

AC code:

#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int> P;
P p[];
bool u[];
int su[];
int num;
void olas()
{
num=;
memset(u,true,sizeof(u));
for(int i=;i<=;i++)
{
if(u[i]) su[num++]=i;
for(int j=;j<num;j++)
{
if(i*su[j]>) break;
u[i*su[j]]=false;
if(i%su[j]==) break;
}
}
}
int main()
{
//freopen("input.txt","r",stdin);
int n;
olas();
int num=;
for(int i=;i<=-;i++)
{
if(u[i]&&u[i+])
{
p[num++]=P(i,i+);
}
}
while(~scanf("%d",&n))
{
printf("(%d, %d)\n",p[n-].first,p[n-].second);
}
return ;
}

UVA 10395 素数筛的更多相关文章

  1. 紫书 习题 10-4 UVa 1644(素数筛)

    素数筛没什么好说的 #include<cstdio> #include<vector> #include<cstring> #define REP(i, a, b) ...

  2. Help Hanzo (素数筛+区间枚举)

    Help Hanzo 题意:求a~b间素数个数(1 ≤ a ≤ b < 231, b - a ≤ 100000).     (全题在文末) 题解: a~b枚举必定TLE,普通打表MLE,真是头疼 ...

  3. 素数筛 poj 2689

    素数筛 #include<stdio.h> #include<string.h> #include<algorithm> using namespace std; ...

  4. BestCoder Round #85 hdu5778 abs(素数筛+暴力)

    abs 题意: 问题描述 给定一个数x,求正整数y,使得满足以下条件: 1.y-x的绝对值最小 2.y的质因数分解式中每个质因数均恰好出现2次. 输入描述 第一行输入一个整数T 每组数据有一行,一个整 ...

  5. poj 3048 Max Factor(素数筛)

    这题就是先写个素数筛,存到prime里,之后遍历就好,取余,看是否等于0,如果等于0就更新,感觉自己说的不明白,引用下别人的话吧: 素数打表,找出20000之前的所有素数,存入prime数组,对于每个 ...

  6. Codeforces Round #257 (Div. 1) C. Jzzhu and Apples (素数筛)

    题目链接:http://codeforces.com/problemset/problem/449/C 给你n个数,从1到n.然后从这些数中挑选出不互质的数对最多有多少对. 先是素数筛,显然2的倍数的 ...

  7. Light oj 1197 - Help Hanzo (素数筛技巧)

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1197 给你a和b求a到b之间的素数个数. 先在小区间素数筛,大区间就用类似素数筛的想法 ...

  8. 素数筛&&欧拉筛

    折腾了一晚上很水的数论,整个人都萌萌哒 主要看了欧拉筛和素数筛的O(n)的算法 这个比那个一长串英文名的算法的优势在于没有多次计算一个数,也就是说一个数只筛了一次,主要是在%==0之后跳出实现的,具体 ...

  9. SDUT Fermat’s Chirstmas Theorem(素数筛)

    Fermat's Chirstmas Theorem Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描写叙述 In a letter ...

随机推荐

  1. LiteDB源码解析系列(2)数据库页详解

    在这一篇里,我将用图文的方式展示LiteDB中页的结构及作用,内容都是原创,在描述的过程中有不准确的地方烦请指出. 1.LiteDB页的技术工作原理 LiteDB虽然是单个文件类型的数据库,但是数据库 ...

  2. python整型-浮点型-字符串-列表及内置函数(上)

    整型 简介 # 是否可变类型: 不可变类型 # 作用:记录年龄.手机号 # 定义: age = 18 # --> 内部操作 age = int(18) # int('sada') # 报错 in ...

  3. JSON的简单使用之提取多层嵌套的JSON(C#)

    JSON.NET(http://json.codeplex.com/)使用来将.NET中的对象转换为JSON字符串(序列化?),或者将JSON字符串转换为.NET中已有类型的对象(反序列化?) 反序列 ...

  4. 《VR入门系列教程》之5---应用方向

    VR应用方向     面向消费者的虚拟现实才发展了几年,就出现了大量应用程序,虚拟现实抓住了人们对未来的渴望.开发者甚至想要把整个现实世界都做成虚拟现实,这些都是可以理解的.     但是,现在仍然没 ...

  5. laravel 5.6初学笔记

    laravel 5.6初学笔记 http://note.youdao.com/noteshare?id=bf4b701b49dd035564e7145ba2d978b4 框架简介 laravel文档齐 ...

  6. python课堂整理15---map, filter,reduce函数

    一.map函数 处理序列(可迭代对象)中的每一个元素,得到的结果是一个‘列表’(其实是个迭代器),该‘列表’元素个数及位置与原来一样 理解下面这段代码: num_l = [1, 2, 4, 6] de ...

  7. Linux系统管理----磁盘管理与文件系统

    1.为主机新增两块30GB的SCSI硬盘 找到要添加的虚拟机,单击鼠标右键,点击设置 点击添加 选择硬件类型,然后点击下一步 选择要创建的磁盘类型,然后点击下一步 指定要创建磁盘的容量,然后点击下一步 ...

  8. 如何选择合适的SSL证书类型

    网站安装SSL证书就可以将http升级为https加密模式,网站安装SSL证书因此成为一种趋势.如何为网站选择适合的SSL证书类型呢? SSL证书类型可分为2大类:1)按照验证方式分类2)按照支持域名 ...

  9. java高并发系列 - 第22天:java中底层工具类Unsafe,高手必须要了解

    这是java高并发系列第22篇文章,文章基于jdk1.8环境. 本文主要内容 基本介绍. 通过反射获取Unsafe实例 Unsafe中的CAS操作 Unsafe中原子操作相关方法介绍 Unsafe中线 ...

  10. redhat linux 5.3修改Java环境变量

    需要配置的环境变量 1. PATH环境变量.作用是指定命令搜索路径,在shell下面执行命令时,它会到PATH变量所指定的路径中查找看是否能找到相应的命令程序.我们需要把jdk安装目录下的bin目录增 ...