Sunday, August 24, 2014

COJ 1388. Last Digit of A ^ B

We can only have a look at the last digit of each base by ignoring the rest of the number. If we have a closer look at the last digits of powers from bases 0 to 9 we will notice a good pattern. So below is the list of last digits of  powers of numbers from 1 to 9.
1- 1 1 1 1 1
2- 2 4 8 6 2
3- 3 9 7 1 3
4 -4 6 4 6 4
5- 5 5 5 5 5
6- 6 6 6 6 6
7- 7 9 3 1 7
8- 8 4 2 6 8
9- 9 1 9 1 9

One thing we can notice which is true for all numbers is that after every 4th power the last digits are repeating themselves . This means that the answer for the power of 3 and the power of and the power of 11 are the same. So we only need to consider the powers from 1 to 4 and then when outputing the answer we need to only take the remainder of the given power and check for the according last digit. Also don't forget the power=0 case.


       FULL SOURCE CODE 

No comments:

Post a Comment