P22 · 1 second · 256 MB

Modular Exponentiation

Return `a^e mod m`. `e` is a non-negative integer. Treat `0^0` as `1`. `a` may be negative — reduce it into `[0, m)` first. `m` is at least 1.

function modPow(a, e, m) → number

CONSTRAINTS

  • -10⁹ ≤ a ≤ 10⁹
  • 0 ≤ e ≤ 10⁹
  • 1 ≤ m ≤ 10⁶
Input: a = 3, e = 13, m = 1000000007
Output: 1594323
Input: a = 2, e = 10, m = 1000
Output: 24
Input: a = 0, e = 0, m = 7
Output: 1
1
2
3
4
5
6
7
8

Arena judges JavaScript in a Web Worker (1s wall-clock, then TLE). Samples are public; submit runs hidden tests too. C++/Python is a later swarm package.