1. C++, Tobias Fuchs

/* Problem: 10394
* Author: Tobias Fuchs
* Verdict: Accepted
* Runtime: 1.040
*/

#include<iostream>
#include<string>
#include<vector>

#define log(m) ::std::cout << "log: '" << m << "'" << ::std::endl

using ::std::cin;
using ::std::string;

/* max input value is 1.000.000 = 1000 * 1000
*/
#define MAX_PRIME_SQRT 4291
#define MAX_PRIME (MAX_PRIME_SQRT*MAX_PRIME_SQRT)

/* We want linearized access to this map, so
* do not use ::std::vector, but a simple
* 2-dim array of integers.
*/
bool primes[MAX_PRIME_SQRT][MAX_PRIME_SQRT];

/* Provides serialized accesso to map of primes.
* Map is generated using Sieve of Eratosthenes.
*/
inline bool
is_prime(unsigned int n)
{
return (*primes)[n];
}

/* Building map of primes.
* Implements Sieve of Erastothenes.
*/
void
build_primes()
{
unsigned int step = 2;
unsigned int mark = 1;
memset(primes, true, MAX_PRIME);
while(step < MAX_PRIME_SQRT) {
mark = step;
while(mark+step < MAX_PRIME) {
mark += step;
(*primes)[mark] = false;
}
do { ++step; } while(!is_prime(step));
}
(*primes)[0] = false;
(*primes)[1] = false;
}
void
set_to_next_prime(unsigned int * value_ref) {
do { ++(*value_ref); } while(!is_prime((*value_ref)));
}
void
set_to_prev_prime(unsigned int * value_ref) {
do { --(*value_ref); } while(!is_prime((*value_ref)));
}

int
main()
{
unsigned int nr, a;
string nr_str;

build_primes();

::std::vector<unsigned int> twins;

while(cin >> nr_str && (nr = atoi(nr_str.c_str())) != 0)
{
if(twins.size() < nr) {
if(twins.empty()) { a = 0; }
else { a = twins.back()+1; }
for(; twins.size() < nr; ++a) {
if(is_prime(a) && is_prime(a+2)) { twins.push_back(a); }
}
}
::std::cout << "(" << twins[nr-1] << ", " << twins[nr-1]+2 << ")" << ::std::endl;
}
return 0;
}