Number of Theory
Solution of 1086. Cryptography
While preparing this problem set the jury has run into the following problem: it was necessary to send by e-mail the texts of the problems. As it is well known, e-mail is not reliable, messages are sent not enciphered, there is a danger that someone can intercept them. The members of the program committee wanted no participant know the texts of the problems before the start of the contest. That's why they resorted to cryptography methods in order to save the texts of the problems from an unsanctioned reading. The jury gas worked up a new way of enciphering of a text. It is not patented yet, so it's kept secret. However, we'll reveal you one secret: the new algorithm is based on the work with prime numbers. In particular, in uses a calculation of n-th by order prime number.
Several members of the program committee independently have worked up programs that make such calculations, but these programs produce different answers. Each one of the programmers is sure that his program works correctly. That's why the jury has reached the deadlock and can't continue working. The contest is about not to take place.
You are to help to the jury and to save the contest. We want you to write a program that calculates the n-th by order prime number. The main thing is that your program should work correctly.
First line contains a positive integer k. Then k positive integers follow (one in each line). The numbers don't exceed 15000.
For each number n you should output the n-th by order prime number. Each number should be in its line.
Inputcopy | Outputcopy |
---|---|
4 3 2 5 7 | 5 3 11 17 |
The prime number is a positive integer that has exactly two different positive divisors, i.e. 1 is not a prime number.
#include<bits/stdc++.h>
using namespace std;
#define crypto 165000
#define ll long long
ll prim[crypto];
ll fectar[crypto];
void isPrim()
{
ll n = crypto;
int prim_num=1;
ll sqr = sqrt(n);
for(int i=4 ; i<=n; i=i+2)
{
fectar[i] = 1;
}
for(int i=3 ; i<=sqr; i=i+2)
{
if(fectar[i]==0)
{
for(int j=2*i; j<=n; j+=i)
{
fectar[j]=1;
}
}
}
for(int i=2 ; i<=n ; i++)
{
if(fectar[i] == 0)
{
prim[prim_num] = i;
prim_num++;
}
}
}
int main()
{
int k, position;
isPrim();
scanf("%d", &k);
while(k--)
{
cin >> position;
cout << prim[position] << endl;
}
}
Comments
Post a Comment