My Weblog

Blog about programming and math

SPOJ 9032. Cube Free Numbers

SPOJ 9032. Cube Free Numbers is simply sieving and binary search. Remove all the cubes in the given range and do a binary search to get the index. Accepted c++ code .

#define Lim 1000001
using namespace std;
bool c[Lim];
vector<int> v;

void iscube()
          for(int i=2;i*i*i<Lim ; i++) 
            int k = i*i*i;
            for(int j = k ; j<Lim ; j+=k) c[j] = 1 ;
         for(int i=1;i<Lim ; i++) if (!c[i]) v.push_back(i);     
         //for(int i=1;i<20;i++) cout<<v[i]<<" ";cout<<endl;  
     int main()
         int t,k,x=1;
            if(k==1) { printf("Case %d: 1\n", x++) ; continue;} 
            if(c[k]) printf("Case %d: Not Cube Free\n", x) ;
            else cout<<"Case "<<x<<":"<<" "<<lower_bound(v.begin(),v.end(),k)-v.begin()<<endl;                           


June 15, 2011 - Posted by | Programming | ,


  1. Even binary search is optional , declare one more array of Lim size to store index of cube-free numbers.

    Comment by divyanshu | June 15, 2011 | Reply

  2. Thanks for suggestion Divyanshu.

    Comment by tiwari_mukesh | June 15, 2011 | Reply

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: