Translate

Friday, 23 January 2015

TDKPRIME spoj solution.....


HII .......If u know optimized sieve then this is easy problem...

Here simple u have to precompute...........and store all the k-th terms.....


Here is my ac c++ solution is>>>>>>>>>>
IF U HAVE PROBLEM IN THIS LOGIC PLEASE ASK ME......IN BELOW COMMENT......

#include<cstdio>
#include<iostream>
#include<vector>
using namespace std;
#define lim 86029000
vector<bool> b((lim>>1)+100);
int p[5000100];
void prime()
{
 for(int i=3;i<=9257;i+=2)
 {
  if(b[(i-3)>>1]==0)
  {
   for(int j=i*i;j<lim;j+=2*i)
   {
     b[(j-3)>>1]=1;
   }
  }
 }
  p[0]=2;
  int count=1;
  for(int i=0;2*i<lim;i++)
   if(!b[i])
    p[count++]=2*i+3;
}
int main()
{
      prime();
        int t,q;
  scanf("%d",&t);
  while(t--)
  {
   scanf("%d",&q);
   printf("%d\n",p[q-1]);
  }
 return 0;
}

3 comments:

Unknown said...

how do u precompute the primes ? how does the function work ? i am not able to understand

Unknown said...

Why are you taking i till 9257? can you please explain. Thanks in advace.

Unknown said...

and also why u have taken "lim" as 86029000

Working With Java Collections