Here you will find solutions of many problems on spoj. If you want solution of some problem which is not listed in blog or have doubt regarding any spoj problem (which i have solved) or any programming concept (data structure) you can mail me @ raj.nishant360@gmail.com

And my humble request to you all that don't copy the code only try to understand the logic and algorithm behind the code. I have started this because if you tried as hard as you can and still can't find any solution to the problem then you can refer to this.
You can read my answer how to start competitive programming CLICK HERE

Thursday, February 6, 2014

CUBEFR-Cube Free Numbers

below given code is for cubefr spoj or Cube Free Numbers spoj.
here u nave to simply apply sieve .
#include<stdio.h>
int A[1000001]={0};
void cube_free()
{
 int i,k,j;
 for(i=2;i<=100;i++)
 {
  if(A[i]==0)
  {
   k=i*i*i;
   for(j=k;j<=1000000;j+=k)
    A[j]=-1;
  }
 }
 k=1;
 for(i=1;i<=1000000;i++)
  if(A[i]==0)
   A[i]=k++;
}
int main()
{
 cube_free();
 int t,n;
 scanf("%d",&t);
 for(int i=1;i<=t;i++)
 {
  scanf("%d",&n);
  if(A[n]!=-1)
   printf("Case %d: %d\n",i,A[n] );
  else
   printf("Case %d: Not Cube Free\n",i);
 }
 return 0;
}

No comments:

Post a Comment

Your comment is valuable to us