Skip to content
BytePatterns

Power With a Huge Exponent

MediumMath & Number Theory#fast-exponentiation#modular-arithmetic~25m

Problem

A checksum scheme needs a^b mod 1337, where a is a positive integer up to 2^31 - 1 and the exponent b is so large it arrives as a list of its decimal digits, most significant first, up to 2,000 digits long. Return the result. Turning the digit list into one integer and calling a general power routine is not allowed; work from the digits directly.

Examples

Input:  a = 2, b = [3]
Output: 8
Input:  a = 2, b = [1, 0]
Output: 1024
Why:    the exponent is 10, and 2^10 = 1024 is still below 1337
Input:  a = 1, b = [4, 3, 3, 8, 5, 2]
Output: 1
Why:    edge case, 1 to any power is 1

Hints

0 / 3

Stuck on the idea rather than the code? Fast Exponentiation covers it.