Skip to content

Commit 0c65c47

Browse files
committed
2961. Double Modular Exponentiation
1 parent 4e1ffc4 commit 0c65c47

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
class Solution {
2+
3+
// Solution by Sergey Leschev
4+
// 2961. Double Modular Exponentiation
5+
6+
func getGoodIndices(_ variables: [[Int]], _ target: Int) -> [Int] {
7+
var result: [Int] = []
8+
9+
func power(_ b: Int, _ p: Int, _ m: Int) -> Int {
10+
if p <= 0 {
11+
return 1
12+
}
13+
var t = power(b, p / 2, m)
14+
t = ((t * t) % m)
15+
return (p % 2 == 1) ? (t * b) % m : t
16+
}
17+
18+
for i in 0..<variables.count {
19+
let a = variables[i][0] % 10
20+
let b = variables[i][1]
21+
let c = variables[i][2]
22+
let m = variables[i][3]
23+
24+
var t = power(a, b, 10)
25+
t = power(t, c, m)
26+
27+
if t == target {
28+
result.append(i)
29+
}
30+
}
31+
32+
return result
33+
}
34+
}

0 commit comments

Comments
 (0)