UVA 10395 素数筛
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 素数筛的更多相关文章
- 紫书 习题 10-4 UVa 1644(素数筛)
素数筛没什么好说的 #include<cstdio> #include<vector> #include<cstring> #define REP(i, a, b) ...
- Help Hanzo (素数筛+区间枚举)
Help Hanzo 题意:求a~b间素数个数(1 ≤ a ≤ b < 231, b - a ≤ 100000). (全题在文末) 题解: a~b枚举必定TLE,普通打表MLE,真是头疼 ...
- 素数筛 poj 2689
素数筛 #include<stdio.h> #include<string.h> #include<algorithm> using namespace std; ...
- BestCoder Round #85 hdu5778 abs(素数筛+暴力)
abs 题意: 问题描述 给定一个数x,求正整数y,使得满足以下条件: 1.y-x的绝对值最小 2.y的质因数分解式中每个质因数均恰好出现2次. 输入描述 第一行输入一个整数T 每组数据有一行,一个整 ...
- poj 3048 Max Factor(素数筛)
这题就是先写个素数筛,存到prime里,之后遍历就好,取余,看是否等于0,如果等于0就更新,感觉自己说的不明白,引用下别人的话吧: 素数打表,找出20000之前的所有素数,存入prime数组,对于每个 ...
- Codeforces Round #257 (Div. 1) C. Jzzhu and Apples (素数筛)
题目链接:http://codeforces.com/problemset/problem/449/C 给你n个数,从1到n.然后从这些数中挑选出不互质的数对最多有多少对. 先是素数筛,显然2的倍数的 ...
- Light oj 1197 - Help Hanzo (素数筛技巧)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1197 给你a和b求a到b之间的素数个数. 先在小区间素数筛,大区间就用类似素数筛的想法 ...
- 素数筛&&欧拉筛
折腾了一晚上很水的数论,整个人都萌萌哒 主要看了欧拉筛和素数筛的O(n)的算法 这个比那个一长串英文名的算法的优势在于没有多次计算一个数,也就是说一个数只筛了一次,主要是在%==0之后跳出实现的,具体 ...
- SDUT Fermat’s Chirstmas Theorem(素数筛)
Fermat's Chirstmas Theorem Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描写叙述 In a letter ...
随机推荐
- C#3.0新增功能08 Lambda 表达式
连载目录 [已更新最新开发文章,点击查看详细] Lambda 表达式是作为对象处理的代码块(表达式或语句块). 它可作为参数传递给方法,也可通过方法调用返回. Lambda 表达式广泛用于: 将 ...
- 跟着大彬读源码 - Redis 6 - 对象和数据类型(下)
继续撸我们的对象和数据类型. 上节我们一起认识了字符串和列表,接下来还有哈希.集合和有序集合. 1 哈希对象 哈希对象的可选编码分别是:ziplist 和 hashtable. 1.1 ziplist ...
- python函数对象-命名空间-作用域-02
函数对象 函数是第一对象: # 函数名指向的值可以被当做参数传递 函数对象的特性(*****灵活运用,后面讲装饰器会用到) 函数名可以像变量一样被传递 # 变量可以被传递 name = 'jason' ...
- Java集合 HashSet的原理及常用方法
目录 一. HashSet概述 二. HashSet构造 三. add方法 四. remove方法 五. 遍历 六. 合计合计 先看一下LinkedHashSet 在看一下TreeSet 七. 总结 ...
- 图像相似度中的Hash算法
度量两张图片的相似度有许多算法,本文讲介绍工程领域中最常用的图片相似度算法之一--Hash算法.Hash算法准确的说有三种,分别为平均哈希算法(aHash).感知哈希算法你(pHash) ...
- c#小灶——初识c#
提到c#,就不得不说.net,.net是微软开发的一个平台,简单来说,在这个平台上,可以编写.运行程序.可能很多人觉得这个平台离我们很遥远,其实不然,这个平台就一直在我们的windows操作系统里,默 ...
- 使用verilog编写锁存器与触发器
需要注意的地方有四点: 1.关于锁存器与触发器在原理上的不同点,以及代码的不同点 2.关于高电平有效与低电平有效之前的区别 3.理解实现复位与实现D触发器之间的区别 4.理解同步与异步之间的区别 锁存 ...
- Spring 源码学习(一)-容器的基础结构
关注公众号,大家可以在公众号后台回复“博客园”,免费获得作者 Java 知识体系/面试必看资料 展示的代码摘取了一些核心方法,去掉一些默认设置和日志输出,还有大多数错误异常也去掉了,小伙伴想看详细代码 ...
- ORACLE 的CONNECT BY、START WITH,CONNECT_BY_ROOT、CONNECT_BY_ISLEAF、SYS_CONNECT_BY_PATH,LEVEL的使用(Hierarchical query-层次查询)
如果表中存在层次数据,则可以使用层次化查询子句查询出表中行记录之间的层次关系基本语法: START WITH <condition1> CONNECT BY [ NOCYCLE ] < ...
- 理解分布式一致性与Raft算法
理解分布式一致性与Raft算法 永远绕不开的CAP定理 出于可用性及负载方面考虑,一个分布式系统中数据必然不会只存在于一台机器,一致性简单地说就是分布式系统中的各个部分保持数据一致 但让数据保持一致往 ...