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....


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...