-
Notifications
You must be signed in to change notification settings - Fork 886
/
SuperPow.swift
32 lines (28 loc) · 892 Bytes
/
SuperPow.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/**
* Question Link: https://leetcode.com/problems/super-pow/
* Primary idea: a * b % k = (a % k) * (b % k) % k
* a ^ b % k = [a ^ (b / 10 * 10) % k] * [a ^ (b % 10) % k] % k
* f(a, b) = f(a, b / 10 * 10) * f(a, b % 10) % k
* = f(f(a, b / 10), 10) * f(a, b % 10) % k
*
* Time Complexity: O(n), Space Complexity: O(1)
*/
class SuperPow {
let base = 1337
func superPow(a: Int, _ b: [Int]) -> Int {
return _superPowHelper(a, b, b.count - 1)
}
private func _pow(a: Int, _ b: Int) -> Int {
var ret = 1
for _ in 0..<b {
ret = ret * a % base
}
return ret
}
private func _superPowHelper(a: Int, _ b: [Int], _ idx: Int) -> Int {
guard idx >= 0 else {
return 1
}
return _pow(_superPowHelper(a, b, idx - 1), 10) * _pow(a, b[idx]) % base
}
}