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:

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. typedef pair<int,int> P;
  4. P p[];
  5. bool u[];
  6. int su[];
  7. int num;
  8. void olas()
  9. {
  10. num=;
  11. memset(u,true,sizeof(u));
  12. for(int i=;i<=;i++)
  13. {
  14. if(u[i]) su[num++]=i;
  15. for(int j=;j<num;j++)
  16. {
  17. if(i*su[j]>) break;
  18. u[i*su[j]]=false;
  19. if(i%su[j]==) break;
  20. }
  21. }
  22. }
  23. int main()
  24. {
  25. //freopen("input.txt","r",stdin);
  26. int n;
  27. olas();
  28. int num=;
  29. for(int i=;i<=-;i++)
  30. {
  31. if(u[i]&&u[i+])
  32. {
  33. p[num++]=P(i,i+);
  34. }
  35. }
  36. while(~scanf("%d",&n))
  37. {
  38. printf("(%d, %d)\n",p[n-].first,p[n-].second);
  39. }
  40. return ;
  41. }

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. app同包同签名不能安装问题

    今天博主与团队之间出现一个很郁闷的问题: 那就是我们开发的一个app,在升级推送版本的时候突然出现,相同的包名.相同的签名.在安装的时候出现,安装签名不一致(安装失败)的提示. 让我们很是困扰.后来发 ...

  2. [蓝桥杯] Fibonacci数列 入门

    原题链接 import java.util.Scanner;//导入Scanner类 public class Main { public static void main(String[] args ...

  3. 第二篇:"空空如也"的博客应用

    文中涉及的示例代码,已同步更新到 HelloGitHub-Team 仓库 建立博客应用 我们已经建立了 django 博客的项目工程,并且成功地运行了它.不过到目前为止这一切都还只是 django 为 ...

  4. mysql8.0的连接写法

    由于mysql8.0的新特新,所以Driver要写成“com.mysql.cj.jdbc.Driver” url:"jdbc:mysql://host_address:3306/db_nam ...

  5. http的无状态

    无状态协议是指协议对务处理没有记忆能力.缺少状态意味着如果后续处理需要前面的信息,则它必须重传,这样可能导致每次连接传送的数据量增大.另一方面,在服务器不需要先前信息时它的应答就较快. Http协议不 ...

  6. Java集合系列(二):ArrayList、LinkedList、Vector的使用方法及区别

    本篇博客主要讲解List接口的三个实现类ArrayList.LinkedList.Vector的使用方法以及三者之间的区别. 1. ArrayList使用 ArrayList是List接口最常用的实现 ...

  7. Split函数的使用

    Split函数,用来返回一个下标从零开始的一维数组,如下举例说明 1.split(' '),''号中间是空格 def break_words(stuff):    """ ...

  8. vue教程二 vue组件(3)

    给属性传递数据 <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> < ...

  9. 【Java】字符串空格相关

    1. 去掉首尾空格 [trim() 方法] String.trim() //去掉首尾空格 2. 替换多个空格为一个 [replaceAll() 方法] str.replaceAll(" + ...

  10. 【iOS】判断苹果的设备是哪种

    有时候需要判断苹果的设备是 iPhone 还是 iPad 等其他设备,示例代码如下: if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUs ...