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
// @Title: 统计数组中相等且可以被整除的数对 (Count Equal and Divisible Pairs in an Array)
// @Author: 15816537946@163.com
// @Date: 2022-02-19 22:47:27
// @Runtime: 24 ms
// @Memory: 3.2 MB
func countPairs(nums []int, k int) int {
    var ans int
    rec := make(map[int][]int)
    for i,v := range nums {
        if rec[v] != nil {
            for _,idx := range rec[v] {
                if (idx*i) % k == 0{
                    fmt.Println(idx,i)
                    ans++
                }
            }

        }
        if rec[v]== nil {
            rec[v] = make([]int,0)
        }
        rec[v] = append(rec[v],i)
    }
    // fmt.Println(rec)
    
    return ans
}