funcnumberOfPairs(nums1 []int, nums2 []int, k int)int64 { ma := map[int]int{} for _, t := range nums1 { if t % k != 0 { continue } t /= k sqrt_ := int(math.Sqrt(float64(t))); for i := 1; i <= sqrt_; i++ { if t % i == 0 { ma[i]++ ma[t / i]++ } } if sqrt_ * sqrt_ == t { ma[sqrt_]-- } } ans := int64(0) for _, t := range nums2 { ans += int64(ma[t]) } return ans }
from typing importList from collections import defaultdict from math import sqrt
classSolution: defnumberOfPairs(self, nums1: List[int], nums2: List[int], k: int) -> int: ma = defaultdict(int) for t in nums1: if t % k: continue t //= k sqrt_ = int(sqrt(t)) for i inrange(1, sqrt_ + 1): if t % i == 0: ma[i] += 1 ma[t // i] += 1 if sqrt_ * sqrt_ == t: ma[sqrt_] -= 1 ans = 0 for t in nums2: ans += ma[t] return ans