\[ \let\oldlor\lor \renewcommand{\lor}{\; \oldlor \;} \let\oldland\land \renewcommand{\land}{\; \oldland \;} \renewcommand{\set}[1]{\{\, #1 \,\}} \renewcommand{\given}{\,\mid\,} \renewcommand{\abs}[1]{\lvert #1 \rvert} \renewcommand{\divs}{\!\;\mid\;\!} \renewcommand{\ndivs}{\!\;\nmid\;\!} \renewcommand{\betw}[3][1]{#1 \leq #2 \leq #3} \renewcommand{\mod}[1]{\ (\mathrm{mod}\ #1)} \renewcommand{\floor}[1]{\left \lfloor #1 \right \rfloor} \renewcommand{\ceil}[1]{\left \lceil #1 \right \rceil} \renewcommand{\t}[1]{\texttt{#1}} \renewcommand{\fori}[2][i]{\text{for } #1 = 0, 1, \dots, #2} \renewcommand{\x}[1]{\text{#1}} \renewcommand\concat{\mathbin{+\mkern-10mu+}} \DeclareMathOperator*{\CONCAT}{\concat} \DeclareMathOperator*{\SCC}{\|} \]
UVa 10394 - Twin Primes
Time limit: 3.0 seconds
UVa Difficulty rating: 2
View on UVa
Problem Description

This is a prime number problem involving the determination of twin primes. We are told twin primes are pairs of primes of the form (p, p+2). For example, the first two twin prime pairs are (3, 5) and (5, 7). The problem description essentially asks the following: Given an integer S, the serial number of a twin prime pair, output the Sth twin prime pair. The restrictions on S are given as follows. 1 <= S <=100000.

Input is given by 10,000 lines or less, each composed of an integer S that specifies the serial number of the target twin prime pair to be output for that case. Each input line described by integer Sth should have a corresponding output line of the form, (p1, p2), where (p1, p2) is the Sth twin prime pair and p1 = p2+2.

In order to solve this problem we need to use the Sieve of Eratosthenes to precompute the primes we need, up to the upper bound specified by the problem. Since each test case will have an integer less than or equal to 100,000, and we are allowed to assume primes in the 100,000th twin prime pair are less than 20,000,000, we need to precompute primes up to 20,000,000. One version of the Sieve I like to use is one that uses std::bitset . The function is written as follows.

Pay close attention to the inner for-loop structure, and make sure you get it right. When under pressure, this is a common place to make typographical errors that lead to frustrating debugging sessions. From main(), we are going to call our sieve function, passing M as a parameter. This computes prime numbers up to M (M = 20,000,000), and stores them in ascending order in our std::vector vp.

Links

C++ References

UVa

230 Borrowers
543 Goldbach's Conjecture
900 Brick Wall Patterns
10047 The Monocycle
10140 Prime Distance
10165 Stone Game
10338 Mischievous Children
10394 Twin Primes
10892 LCM Cardinality

CodeForces

1A Theatre Square
4A Watermelon
25A IQ test
50A Domino piling
58A Chat room
59A Word
69A Young Physicist
71A Way Too Long Words
96A Football
112A Petya and Strings
118A String Task
131A cAPS lOCK
158A Next Round
189A Cut Ribbon
230B T-primes
231A Team
263A Beautiful Matrix
281A Word Capitalization
282A Bit++
318A Even Odds
339A Helpful Maths
339B Xenia and Ringroad
455A Boredom
459B Pashmak and Flowers
474B Worms
479A Expression
486A Calculating Function
489B BerSU Ball
489C Given Length and Sum of Digits...
492B Vanya and Lanterns
500A New Year Transportation
507B Amr and Pins
513A Game
520B Two Buttons
550A Two Substrings
580A Kefa and First Steps
742A Arpa's hard exam and Mehrdad's naive cheat
766B Mahmoud and a Triangle
935A Fafa and his Company
977B Two-gram
977D Divide by three, multiply by two
996A Hit the Lottery
1097A Gennady and a Card Game
1108A Two distinct points
1154A Restoring Three Numbers