ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1086. Cryptography

using sieve method in c++..
Posted by Shuvro Chandra Das 1 Jun 2020 10:48
#include <bits/stdc++.h>
using namespace std;
#define MAX_SIZE 1000005
void SieveOfEratosthenes(vector<int> &primes)
{
    bool IsPrime[MAX_SIZE];
    memset(IsPrime, true, sizeof(IsPrime));
    for (int p = 2; p * p < MAX_SIZE; p++)
    {
        if (IsPrime[p] == true)
        {
            for (int i = p * p; i < MAX_SIZE; i += p)
                IsPrime[i] = false;
        }
    }
    for (int p = 2; p < MAX_SIZE; p++)
    if (IsPrime[p])
            primes.push_back(p);
}

int main()
{
    int t;
    cin>>t;
    vector<int> primes;
    SieveOfEratosthenes(primes);
    while(t--){
        int n;
    cin>>n;
    cout<<primes[n-1]<<endl;
    }
    return 0;
}