modPow function

int modPow(
  1. int base,
  2. int exp,
  3. int modulus
)

Implementation

int modPow(int base, int exp, int modulus) {
  int result = 1;
  int cur = base;
  int e = exp;
  while (e > 0) {
    if ((e & 1) != 0) {
      result = modMul(result, cur, modulus);
    }
    cur = modMul(cur, cur, modulus);
    e >>= 1;
  }
  return result;
}