2961. 双模幂运算

双模幂运算

解法一: 快速幂

go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
func getGoodIndices(variables [][]int, target int) []int {
ans := []int{}
for i, variable := range variables {
a := variable[0]
b := variable[1]
c := variable[2]
m := variable[3]
if quickPow(quickPow(a, b, 10), c, m) == target {
ans = append(ans, i)
}
}
return ans
}

func quickPow(num, pow, mod int) int {
if pow == 0 {
return 1
}
half := quickPow(num, pow>>1, mod)
if (pow & 1) == 0 {
return (half * half) % mod
}
return (half * half * num) % mod
}
作者

wuhunyu

发布于

2024-07-30

更新于

2025-01-15

许可协议