Spoj TDKPRIME - Finding the Kth Prime Solution




Problem Link: https://www.spoj.com/problems/TDKPRIME/

 Solution in C++: 

///La ilaha illellahu muhammadur rasulullah
///******Bismillahir-Rahmanir-Rahim******///
///Abul Hasnat  Tonmoy
///Department of CSE,23rd batch
///Islamic University,Bangladesh
///**********ALLAH IS ALMIGHTY************///
#include<bits/stdc++.h>
using namespace std;
vector<bool>v(100000000,true);
int ar[80000000];
int main()
{
    long long int n=100000000,i,j;
    v[1]=v[0]=false;
    for(i=4; i<=n; i+=2)v[i]=false;
    for(i=3; i*i<=n; i+=2)
    {
        if(v[i])
        {
            for(j=i*i; j<=n; j=j+(2*i))
            {
                v[j]=false;
            }
        }
    }
    j=0;
    ar[0]=2;
    for(i=3; i<=n; i+=2)
    {
        if(v[i])
            ar[++j]=i;
    }
    int t,q;
    cin>>t;
    while(t--)
    {
        cin>>q;
        cout<<ar[q-1]<<endl;
    }
    return 0;
}












No comments

Most View Post

Recent post

Codeforces Round 971 (Div. 4) 2009C. The Legend of Freya the Frog Solution

  Problem Link    https://codeforces.com/contest/2009/problem/C S olution in C++: /// Author : AH_Tonmoy #include < bits / stdc ++. h &g...

Powered by Blogger.