Algorithm
problem link- https://www.spoj.com/problems/LSTDGT/
LSTDGT - Last Digit of Power
Given two numbers a and b. Find the last digit of a^b. (a to the power b).
Input
There will be t (1 <= t <= 10) test cases. In each case there will be two number a (1 <= a <= 10^100000) and b (1 <= b <= 10^100000) separated by a space.
Output
Print the last digit of a^b in a single line.
Example
Input: 2 3 10 6 2 Output: 9 6
Code Examples
#1 Code Example with Python Programming
Code -
Python Programming
period = [0] * 10
for i in range(1, 10):
now = i
for j in range(10):
now = now * i % 10
if now == i:
period[i] = j + 1
break
T = int(input())
for _ in range(T):
a, b = raw_input().split()
a = int(a[-1])
b = int(b) % period[a]
if b == 0:
b = period[a]
result = a
for i in range(b - 1):
result = result * a % 10
print(result)
Copy The Code &
Try With Live Editor
Input
2
3 10
6 2
3 10
6 2
Output
9
6
6
Demonstration
SPOJ Solution-Last Digit of Power-Solution in C, C++, Java, Python