LASTDIG
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.
import math
t=input()
for i in range(t):
s=raw_input()
str=s.split(' ')
a=int(str[0])
b=int(str[1])
if(b==0):
print 1
else:
a=a%10
b=b%4
if(b==0):
b=4
print pow(a,b)%10
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.
CODE
import math
t=input()
for i in range(t):
s=raw_input()
str=s.split(' ')
a=int(str[0])
b=int(str[1])
if(b==0):
print 1
else:
a=a%10
b=b%4
if(b==0):
b=4
print pow(a,b)%10
why b = b % 4?
ReplyDeletejust get the md = b % 4
ReplyDeleteif md == 0 then md = 4
and multiply a md times
after that get the last digit
by taken from spoj.com
Can someone explain it?
ReplyDelete