UVa 374 - Big Mod

374 - Big Mod

 * http://acm.uva.es/p/v3/374.html

Summary
A text book problem.

Explanation
We are asked to compute $$b^p \bmod m$$. Using Repeated Squaring, we can calculate $$b^p$$ efficiently, so long as the individual multiplications aren't expensive. Just our luck, the modulus in the expression will keep the final result low. We can use the relation that $$( x \times y ) \bmod m = ((x \bmod m) \times (y \bmod m)) \bmod m$$ to keep the intermediate results small. Thus, we can use a basic integer to hold intermediate results, and never have to resort to BigNum multiplications.

Input
3 18132 17

17 1765 3

2374859 3029382 36123

Output
13 2 13195