পৃষ্ঠাসমূহ

রবিবার, ২১ ফেব্রুয়ারী, ২০১৬

Timus Online Judge 1086. Cryptography Solution

1086. Cryptography
Language: c++


#include <iostream>
#include <vector>
#define M 1000000
using namespace std;
bool marked[M];

bool isPrime(int n)
{
    if (n < 2) return false;
    if (n == 2) return true;
    if (n % 2 == 0) return false;
    return marked[n] == false;
}

void sieve(int n)
{
    for (int i = 3; i * i <= n; i += 2)
    {
        if (marked[i] == false)
        {
            for (int j = i * i; j <= n; j += i + i)
            {
                marked[j] = true;
            }
        }
    }
}
int main()
{

    int k,num;
    cin>>k;
    sieve(1000000);

    vector<int> result;

    for(int i = 0; i<1000000; i++)
        if(isPrime(i))
            result.push_back(i);
    while(k-->0)
    {

        cin>>num;
        cout<<result[num-1]<<endl;


    }

}


৩টি মন্তব্য: