Friday, 7 August 2015

SPOJ: LASTDIG Solution

SPOJ Solution Of LASTDIG

Source Code.....

#include<stdio.h>
#include<stdlib.h>
using namespace std;
int main()
{
    int a2[10][4]={{0,0,0,0},{1,1,1,1},{6,2,4,8},{1,3,9,7},{6,4,6,4},{5,5,5,5},{6,6,6,6},{1,7,9,3},{6,8,4,2},{1,9,1,9}};// last digits of all numbers when
                                  // repeatedly multiplied by its own number
    long int b;
    int a,t,l,z=0;
    for(scanf("%d",&t);t>0;t--)
    {   z=0;
        scanf("%d%ld",&a,&b);
        l=a%10;  //Get last digit of the number
        z=a2[l][b%4]; //Get last digit of a raised to power b
         if(a!=0 && b==0)  //condition when number is raised to the power zero.
            printf("1\n");
         else
        printf("%d\n",z);
    }
    return 0;
}

EXPLAINATION:  
Note that each number will repeat some set of numbers on being raised to some power. Therefore ,
keeping all the set in a matrix.Try keeping variable names as small as possible because Space Limit is just 700Bytes. :) 
Also erase any extra space in code or output ..




SPOJ : DIVSUM Solution

Spoj:  DIVSUM Solution


#include<stdio.h>
#include<stdlib.h>
#include<iostream>

using namespace std;

int main()
{
int divisor[500002];    //creating array to store divsum 
                                     //of all numbers till 500001 
int t,i=0,j=0,n=0;

for(i=1;i< 500002;i++)
{
    for(j=2*i;j< 500002; j+=i){
        divisor[j]+=i;                  
    }                                  
}

for(scanf("%d",&t);t>0;t--){
    scanf("%d",&n);
    printf("%d\n",divisor[n]);
}
return 0;
}

EXPAINATION :
  To create the array of divsum ,  do it the same way as in Sieve of Eratosthenes. That is, first mark all multiple of 2 except 2*1 ,   then mark all multiples of 3 except 3*1 i.e starting from 3*2 and then mark all multiples of 4 except 4*1 ie starting fron 4*2 and so on....


Tuesday, 14 July 2015

SPOJ : CANDY 1 solution

EXPLANATION :

This one has a simple way to solve just by using averages.
Calculate the sum of all candies, now if it can be completely 
divided by total no. of packets , i.e remainder of sum/total no. 
of packets , candies can be equally divided into each packet.
Now get the average , and find out the moves.. 

#include<stdio.h>
#include<stdlib.h>
#include<iostream>
using namespace std;

int main()
{

    int n,t,sum=0,rem=0,avg=0,total=0;
   
    while(1)
    {
     scanf("%d",&n); // n is no. of packets
     sum=0;
     if(n==-1)   //to end inputting
        break;
     int candy[n];//array for no. of candy in each packets
     for(t=0;t<n;t++)
     {
         scanf("%d",&candy[t]);
         sum+=candy[t];
     }

     rem=sum%n;
     avg=sum/n;
     if(rem != 0)//means it cannot be divided equally
     {
         printf("-1\n");
     }
     else    // else get the total no. of moves by using averages
        {
          total=0;
          for(t=0;t<n;t++)
          {
              if(candy[t]>avg)
                total=total+(candy[t]-avg);
          }
          printf("%d\n",total);
    }//else closed
    }
    return 0;
}

Saturday, 11 July 2015

SPOJ: JULKA solution

#include <iostream>
#include <string.h>
#include <stdio.h>

#define maxi 110
using namespace std;
char total[maxi],  diff[maxi], klaudia[maxi], natalia[maxi];

//function  to divide by 2
void divideByTwo(char tem[],int l,char name[])
{
    int i,j,f,a,b;
    for(i=l-1,a=j=f=0;i>=0;i--)
    {b=(a*10+tem[i]-'0')/2;
     a=(a*10+tem[i]-'0')%2;
     if(b)
            f=1;
     if(f)
        name[j++]=b+'0';
    }
    if(!j)
        j++;
    name[j]=0;
}

void calculate()
{
    //add (total+diff)
    char temp[maxi];
    int lenTotal=strlen(total);
    int lenDiff=strlen(diff);
    int i=0,j=0,k=0,a=0,b=0,c=0;
    for(i=lenTotal-1,j=lenDiff-1; i>=0||j>=0||c ;i--,j--,k++)
    {
        a= i>=0?total[i]-'0' : 0;       //get each number of total
        b= j>=0?diff[j]-'0' : 0;        //get each number of diff
        temp[k]=(a+b+c)%10 +'0';   // add both no.
        c=(a+b+c)/10;                       //carry generated
    }
    temp[k]=0;
    strcpy(klaudia,"0");
    divideByTwo(temp,k,klaudia);          // now divide (total+diff)/2

    ///subtract  (total- diff)

    for(i=lenTotal-1,j=lenDiff-1,k=c=0;i>=0;i--,j--,k++)
    {
        a=total[i]-'0';            //get each no. of total
        b=j>=0?diff[j]-'0':0;  //get each no.of diff
                                            //subtracting
        if(a<b+c)              
        {
           temp[k]=(10+a-b-c)+'0';
           c=1;
        }
        else
        {
            temp[k]=a-b-c + '0';
            c=0;
        }
    }
    temp[k]=0;

    strcpy(natalia,"0");
    divideByTwo(temp,k,natalia);      //   now divide (total-diff)/2
}




int main()
{
    int i=0;
    for(i=0;i<10;i++)
    {
        scanf("%s %s",total,diff);
        calculate();
        printf("%s\n%s\n",klaudia,natalia);
    }

    return 0;
}

SPOJ : MARBLES Solution

Question  MARBLES - Marbles Explaination.... Fill n spaces , with k colors. First select k spaces with k-different color.  Now we are l...