UVa 10394 Twin Primes (孪生素数)

10394 - Twin Primes

Time limit: 3.000 seconds

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1335

Twin primes are pairs of primes of the form (p, p+2). The term "twin prime" was coined by Paul Stckel (1892-1919). The first few twin primes are (3, 5), (5, 7), (11, 13), (17, 19), (29, 31), (41, 43). In this problem you are asked to find 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 file is terminated by end of file.

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)

水题。

完整代码:

/*0.342s*/

#include <cstdio>
const int maxn = 20000000;  

bool vis[maxn];
int prime[maxn / 10], c, TwinPrime[100005];  

/*以i为基准筛合数*/
/*O(maxn)时间复杂度*/
void cal_prime()
{
    for (int i = 2; i < maxn; ++i)
    {
        if (!vis[i]) prime[c++] = i;
        for (int j = 0; j < c && i * prime[j] < maxn; ++j)
        {
            vis[i * prime[j]] = true;
            if (i % prime[j] == 0) break;
        }
    }
}  

/*判断孪生素数*/
inline void judge()
{
    int i, j;
    for (i = 3, j = 1; j <= 100000; i += 2)
        if (!vis[i] && !vis[i + 2])
            TwinPrime[j++] = i;
}  

int main()
{
    cal_prime();
    judge();
    int n;
    while (~scanf("%d", &n))
        printf("(%d, %d)\n", TwinPrime[n], TwinPrime[n] + 2);
    return 0;
}

作者署名:csdn博客 synapse7

查看本栏目更多精彩内容:http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索input
, 素数
, prime
, twin
, The
problem
twin primes、10394、10394违章代码、change 10394 closed、primes,以便于您获取更多的相关知识。

时间: 2024-09-28 03:04:40

UVa 10394 Twin Primes (孪生素数)的相关文章

UVa 10066 The Twin Towers:DP&amp;amp;LCS

10066 - The Twin Towers Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=114&page=show_problem&problem=1007 水. 完整代码: /*0.012s*/ #include<bits/stdc++.h> using namespace std; int a

UVa 10236 The Fibonacci Primes:斐波那契素数

10236 - The Fibonacci Primes Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1177 注意此题的描述和维基百科上Fibonacci Prime的描述不同. 上面加着重符的文字说明:可以用类似素数筛的方式筛出Fibonacci Pr

UVa 10168 Summation of Four Primes:“1+1+1+1”问题

10168 - Summation of Four Primes Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1109 思路:既然1+1都在很大的数据范围内成立,那我们不妨先分出两个素数来,然后把剩下的数分成两个素数. 完整代码: 01./*0.025s*

UVa 543 Goldbach&#039;s Conjecture:素数&amp;amp;哥德巴赫猜想

543 - Goldbach's Conjecture Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=484 In 1742, Christian Goldbach, a German amateur mathematician, sent a letter

UVa 113 / POJ 2109 Power of Cryptography

使用double处理大整数&泰勒公式与误差分析 113 - Power of Cryptography Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=99&page=show_problem&problem=49 http://poj.org/problem?id=2109 Background Curre

求助一到素数题/uva,543

问题描述 求助一到素数题/uva,543 Problem A : Goldbach's Conjecture From: UVA, 543Submit Time Limit: 3000 MS In 1742, Christian Goldbach, a German amateur mathematician, sent a letter to Leonhard Euler in which he made the following conjecture: Every number great

UVa 10602

链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=1543 类型:贪心 原题: Company Macrohard has released it's new version of editor Nottoobad, which can understand a few voice commands.

UVa 10392 Factoring Large Numbers:素因子分解

10392 - Factoring Large Numbers Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=100&page=show_problem&problem=1333 One of the central ideas behind much cryptography is that factoring

UVa 10182 Bee Maja:规律&amp;amp;O(1)算法

10182 - Bee Maja Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1123 Maja is a bee. She lives in a bee hive with thousands of other bees. This bee hive c