COINLNUM - Editorial

Problem Link:

Practice

Setter: saquib ul hassan

Difficulty:
EASY-MEDIUM

Prerequisites:
Array

Problem:
We are given two numbers (a and b) and told him to find out the number a^b where ^ represents exponentiation operator and print the last digit of the result i.e ignore the first n-1 digit of the answere.

Quick Explanation:
Since number are very large we store them as a string.
Take last digit in base a.
Now calculate b%4. Here b is very large so we follow this approach to calculate mod of large number.
If b%4==0 that means b is completely divisible by 4, so our exponent now will be exp = 4 because by multiplying number 4 times, we get the last digit according to cycle table in above diagram.
If b%4!=0 that means b is not completely divisible by 4, so our exponent now will be exp=b%4 because by multiplying number exponent times, we get the last digit according to cycle table in above diagram.
Now calculate ldigit = pow( last_digit_in_base, exp ).
Last digit of a^b will be ldigit%10.

SOLUTION:

Setter