Fast Pow
Take 2^5
for example, $2^5 = 2^{4+1} = 2^4 * 2^1$
Initially, base = 2, ans = 1, exp = 5 = (101)
Here, the rightmost 1
means that we should multiply ans
by 2^1
.
The middle 0
means that we should NOT multiply ans
by 2^2
.
The leftmost 1
means that we should multiply ans
by 2^4
.
In sum, we can keep doing exp >>= 1
and base = (base * base)
. We only do ans = (ans * base)
when exp & 1 == 1
.
Implementation
Or simply pre-compute those pows.
If we want to reuse the result across test cases, we can use static vector
.
Problems
Last updated