3442 - The last digit
Nestor was doing the work of his math class about three days but he is tired of make operations a lot and he should deliver his task tomorrow. His math’s teacher gives two numbers a and b. The problem consist in find the last digit of the potency of base a and index b. Help Nestor with his problem. You are given two integer numbers: the base a (0 <= a <= 20) and the index b (0 <= b <= 2,147,483,000), a and b both are not 0. You have to find the last digit of a^b.
This is a Modular exponentiation problem. It also has a high exponent.
The classic method of calculating exponentiation:
- Start your output number at 1.
- If the exponent is odd, multiply the output by the base, and subtract 1 from the exponent.
- Divide the exponent by 2, and multiply the base by itself.
- If the exponent is still greater than 0, loop back to step 2.
Since the result will never be greater than 10, you can apply modulus to the base and output at each step to keep the nubmers small.
- Nothing special. If you followed the algorithm correctly and you get the stock input and output match, the program will be accepted.
- Anything special to note about the problem.
Notes/Hints on actual implementation here.
2 3 10 6 2
- Reference 1