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