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