Problems > Power Tower Modulo

Power Tower Modulo

Difficulty: 2400 6000 ms 128 MB

number-theory

Let T = aa···a be a tower of K copies of a. Print T modulo M.

Input: one line with three integers a, K, M.

Output: T mod M.

Constraints:

  • 1 ≤ a ≤ 109
  • 0 ≤ K ≤ 30
  • 1 ≤ M ≤ 109

Sample Tests

Input 1
2 0 5
Output 1
1
Input 2
2 3 100
Output 2
16
Input 3
3 2 7
Output 3
6
Input 4
2 2 1000
Output 4
4

Read the Editorial

Discussion (opens after you solve — no spoilers before)

No comments yet.

Submit Your Solution

Log in or create a free account to run and submit code.