This page is still under construction.

Parts of this page are still being built. What you see may change.

Last Digits

Interview

Time limit2sMemory limit128 MB

Summary
For each test case, print the last n digits (with leading zeros) of the power tower of height i with base b.
Level

Medium6 of 10

Topics
Number theory, Math, Divide and conquer, Recursion
Solved
No attempts yet

Problem

Raising a number to repeated powers can produce a very large value.

You are given integers bb, nn, and ii. The function ff is defined by

f(x)=bf(x−1)(x>0),f(0)=1f(x) = b^{f(x-1)} \quad (x > 0), \qquad f(0) = 1

so f(i)f(i) is a power tower of height ii with base bb. Write a program that finds the last nn digits of f(i)f(i).

Input

The input consists of several test cases. Each test case consists of three lines: the first line contains bb (1≤b≤1001 \le b \le 100), the second line contains ii (1≤i≤1001 \le i \le 100), and the third line contains nn (1≤n≤71 \le n \le 7). A single line containing 00 follows the last test case and marks the end of input.

Output

For each test case, print the last nn digits of f(i)f(i) on its own line. If f(i)f(i) has fewer than nn digits, pad it with leading zeros so that exactly nn digits are printed.

Examples1

  1. Example 1

    Input
    2
    4
    7
    10
    10
    6
    3
    10
    7
    0
    
    Expected output
    0065536
    000000
    4195387