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 ...
随机推荐
- HBase的优化
HBase的优化 高可用 在 HBase 中 Hmaster 负责监控 RegionServer 的生命周期,均衡 RegionServer 的负载,如果 Hmaster 挂掉了,那么整个 HBase ...
- idea导入ssm javaweb maven项目
本文笔者辛苦整理, 除了为方便大家贴的maven安装配置和方便的现有项目, 如转载请注明: https://www.cnblogs.com/m-yb/p/11229320.html idea导入ssm ...
- [PTA] L3-015 球队“食物链”
原题链接 思路: 如果有环,则起点一定为"1".如果没有可以胜过"1"的,则无环. 根据W,L来建立图,用dfs从1节点遍历+回溯. 剪枝:dfs到某个子序列时 ...
- oracle分隔字符串列转行
1. DEMO: SELECT REGEXP_SUBSTR('1,2', '[^,]+', 1, LEVEL) FROM DUAL CONNECT BY REGEXP_ ...
- python常见模块-collections-time-datetime-random-os-sys-序列化反序列化模块(json-pickle)-subprocess-03
collections模块-数据类型扩展模块 ''' 在内置数据类型(dict.list.set.tuple)的基础上,collections模块还提供了几个额外的数据类型:Counter.deque ...
- 使用java Apache poi 根据word模板生成word报表
项目开发过程中,客户提出一堆导出报表的需求,需要导出word格式,页眉还需要加上客户公司的logo,试了几种方案,最后选择了用 Apache poi 加上自定义标签的方式实现. 目前功能还比较简单,一 ...
- 今天来聊Java ClassLoader
背景 类加载机制作为一个高频的面试题经常会在面试中被问到,前几天一个电话面试就问到,之前有了解过,但是没有梳理成自己的体系,所以说的有点凌乱,今天花点时间整理一下,分享给大家同时自己也好好梳理一下,顺 ...
- Python 学习笔记(1)Python容器:列表、元组、字典与集合
Python容器:列表.元组.字典与集合 列表: 1.列表 的创建 使用[ ] 或者 list()创建列表:empty_list = [ ] 或者 empty_list= list() 使用list( ...
- IO-文件输出流
一.输出流的原理 Java向文件中写数据的原理 Java程序-->JVM(java虚拟机)-->OS(操作系统)-->OS调用写数据的方法-->把数据写入到文件中 tips: ...
- to_string()函数(C++)
to_string函数,这是C++11新增的,使用非常方便,简单查了下:C++11标准增加了全局函数std::to_string 函数原型:string to_string (int val);str ...