A rolling checksum repeatedly raises a base to large powers under a modulus. Given three integers a, b, and m, compute (a ** b) mod m, the remainder when a raised to the power b is divided by m.
By convention a ** 0 = 1 for every a (including a = 0). The result must be the non-negative remainder in the range [0, m - 1]. Because b can be enormous, computing a ** b directly is infeasible — reduce modulo m as you go.
A single line with three space-separated integers a, b, and m.
A single integer: (a ** b) mod m.
Note: when m = 1 every remainder is 0.
Example 1
Input
2 10 1000
Expected
24
Explanation
2 ** 10 = 1024, and 1024 mod 1000 = 24.
Example 2
Input
3 0 7
Expected
1
Explanation
Any base to the power 0 is 1, and 1 mod 7 = 1.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →