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

Monday, February 3, 2014

NOSQ-No Squares Numbers

No Squares Numbers

given below code is for nosq spoj or no squares numbers spoj.
#include <stdio.h>
int A[100000][11]={0};
void sqfree()
{
 int i,temp;
 int k,j;
 A[0][0]=1;
 for(i=2;i<=316;i++)
 {
  if(A[i][10]==0)
  {
   k=i*i;
   for(j=k;j<=100000;j+=k)
     A[j][10]=1;
  }
 }
 
 for(i=1;i<=100000;i++)
 {
  if(A[i][10]==0){
   temp=i;
   while(temp!=0)
   {
    A[i][temp%10]=1;
    temp/=10;
   }
   for(j=0;j<=9;j++)
   {
    A[i][j]+=A[i-1][j];
   }
  }
  else
   for(j=0;j<=9;j++)
    A[i][j]=A[i-1][j];
 }
}
int main()
{
 int t;
 sqfree();
 scanf("%d",&t);
 while(t--)
 {
  int low,high,digit;
  scanf("%d%d%d",&low,&high,&digit);
  printf("%d\n",A[high][digit]-A[low-1][digit]);
 }
 return 0;
}


1 comment:

  1. Can u please explain the mathematical foundation of computing this matrix for calculation of no of "NO SQUARE NUMBERS"

    ReplyDelete

Your comment is valuable to us