---
# System prepended metadata

title: uva 374 - Big Mod

---

# uva 374 - Big Mod
link: https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=310

```cpp=
#include<iostream>
using namespace std;
typedef long long ll;
ll mod(int x, int y, int k){
    if (y == 0) return 1;
    if (y % 2) return (mod(x, y-1, k) * x) % k;
    ll tmp = mod(x, y/2, k);
    return (tmp*tmp) % k;
}
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    ll b, p, m;
    while(cin >> b >> p >> m){
        cout<< mod(b, p, m) <<endl;
    }
}
```