Translate

Tuesday 3 February 2015

AMR11E(Distinct Primes) spoj problem solution

HII GUYS this is one of the easy problem
just compute small prime number and then check which numbers are lucky number and store them
in ur ans array...then for every testcase just print ans in o(1)

Here is my ac c++ solution is::::



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include<bits/stdc++.h>
using namespace std;
#define lim 3600
vector<int>prime;
vector<bool>check(lim,false);
vector<int>ans(lim);
void pre()
{
 int i,j;
 for(i=3;i<=55;i+=2)
 {
  if(check[i]==false)
  {
   for(j=i*i;j<=3025;j+=2*i)
   check[j]=true;
  }
 }
 prime.push_back(2);
 for(i=3;i<=2700;i+=2)
 {
  if(check[i]==false)
  prime.push_back(i);
 }
}
void dev()
{
 int i,j,x,count=0;
 for(i=30;i<=2664;i++)
 {
  x=0;
  for(j=0;prime[j]<=i;j++)
  {
   if(i%prime[j]==0)
    x++;
   if(x==3) {
    ans[count++]=i;
    break;}
  }
 }
}
int main()
{
 pre();
 dev();
 int t,i,n;
 scanf("%d",&t);
 while(t--)
 {
  scanf("%d",&n);
  printf("%d\n",ans[n-1]);
 }
 return 0;
}

No comments:

Working With Java Collections