给你一个下标从 0 开始长度为 n
的整数数组 nums
和一个整数 k
,请你返回满足 0 <= i < j < n
,nums[i] == nums[j]
且 (i * j)
能被 k
整除的数对 (i, j)
的 数目 。
2176. 统计数组中相等且可以被整除的数对 – 力扣(Leetcode)

思路:
按照题意写就行。
python3实现:
class Solution:
def countPairs(self, nums: List[int], k: int) -> int:
count = 0
i = 0
while i <= len(nums) - 1:
for j in range(i+1, len(nums)):
if (nums[i] == nums[j]) and ((i * j) % k == 0):
count += 1
i += 1
return count
