Given two arrays of integers nums1 and nums2, return the number of triplets formed (type 1 and type 2) under the following rules:

Type 1: Triplet (i, j, k) if nums1[i]2 == nums2[j] * nums2[k] where 0 <= i < nums1.length and 0 <= j < k < nums2.length.
Type 2: Triplet (i, j, k) if nums2[i]2 == nums1[j] * nums1[k] where 0 <= i < nums2.length and 0 <= j < k < nums1.length.

 
Example 1:
Input: nums1 = [7,4], nums2 = [5,2,8,9]
Output: 1
Explanation: Type 1: (1,1,2), nums1[1]^2 = nums2[1] * nums2[2]. (4^2 = 2 * 8). 

Example 2:
Input: nums1 = [1,1], nums2 = [1,1,1]
Output: 9
Explanation: All Triplets are valid, because 1^2 = 1 * 1.
Type 1: (0,0,1), (0,0,2), (0,1,2), (1,0,1), (1,0,2), (1,1,2).  nums1[i]^2 = nums2[j] * nums2[k].
Type 2: (0,0,1), (1,0,1), (2,0,1). nums2[i]^2 = nums1[j] * nums1[k].

Example 3:
Input: nums1 = [7,7,8,3], nums2 = [1,2,9,7]
Output: 2
Explanation: There are 2 valid triplets.
Type 1: (3,0,2).  nums1[3]^2 = nums2[0] * nums2[2].
Type 2: (3,0,1).  nums2[3]^2 = nums1[0] * nums1[1].

Example 4:
Input: nums1 = [4,7,9,11,23], nums2 = [3,5,1024,12,18]
Output: 0
Explanation: There are no valid triplets.

 
Constraints:

1 <= nums1.length, nums2.length <= 1000
1 <= nums1[i], nums2[i] <= 10^5

Difficulty: interview

Starter code:

class Solution:
    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:
        

Solutions:
["class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        \n        def triplets(nums1, nums2):\n            sq = collections.Counter(x * x for x in nums1)\n            num = collections.Counter(nums2)\n            \n            res = 0\n            keys = sorted(num.keys())\n            for j, x in enumerate(keys):\n                if num[x] > 1 and x * x in sq:\n                    res += num[x] * (num[x] - 1) // 2 * sq[x * x]\n                for y in keys[j+1:]:\n                    if x * y in sq:\n                        res += num[x] * num[y] * sq[x * y]\n            return res\n        \n        return triplets(nums1, nums2) + triplets(nums2, nums1)                ", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        \n        count1 = Counter(nums1)\n        count2 = Counter(nums2)\n        nums1 = sorted(count1.keys())\n        nums2 = sorted(count2.keys())\n        tot = 0\n        for (c1, n1, c2, n2) in [(count1, nums1, count2, nums2), (count2, nums2, count1, nums1)]:\n            for a in n1:\n                s = a ** 2\n                for b in n2:\n                    if b > a:\n                        break\n                    if s % b == 0 and s // b in c2:\n                        print((a, b))\n                        if b == a:\n                            if c2[b] >= 2:\n                                tot += c1[a] * comb(c2[b], 2)\n                        else:\n                            tot += c1[a] * c2[b] * c2[s // b]\n        return tot\n                    \n", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        \n        \n        def calc(a : List[int], b : List[int]):\n            from collections import defaultdict\n            freq = defaultdict(int)\n            for ai in a:\n                freq[ai] += 1\n            _ans = 0\n            for bi in b:\n                t = bi * bi\n                for k in freq:\n                    if t % k != 0 or t/k not in freq : continue\n                    elif t/k == k : _ans += freq[k] * (freq[k] - 1)\n                    else : _ans += freq[k] * freq[t/k]\n            return int(_ans/2)\n        return calc(nums1, nums2) + calc(nums2, nums1)\n", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        prod2 = collections.defaultdict(int)\n                                       \n        for i in range(len(nums2)):\n            for j in range(i+1,len(nums2)):\n                prod = nums2[i]*nums2[j]\n                \n                if prod in prod2:\n                    prod2[prod]+=1\n                else:\n                    prod2[prod]=1\n            \n        res = 0\n\n        for e in nums1:\n            \n            if e**2 in prod2:\n                res += prod2[e**2]\n                \n        prod1 = collections.defaultdict(int)\n                                       \n        for i in range(len(nums1)):\n            for j in range(i+1,len(nums1)):\n                prod = nums1[i]*nums1[j]\n                \n                if prod in prod1:\n                    prod1[prod]+=1\n                else:\n                    prod1[prod]=1\n                    \n        for e in nums2:\n            if e*e in prod1:\n                res += prod1[e*e]\n                \n        return res", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        ret = 0\n        dict1 = defaultdict(int)\n        dict2 = defaultdict(int)\n        \n        for j in range(len(nums2)):\n            for k in range(j+1, len(nums2)):\n                dict2[nums2[j] * nums2[k]] += 1\n        \n        for j in range(len(nums1)):\n            for k in range(j+1, len(nums1)):\n                dict1[nums1[j] * nums1[k]] += 1\n                        \n        for i in range(len(nums1)):\n            if nums1[i]**2 in dict2:\n                ret += dict2[nums1[i]**2]\n                        \n        for i in range(len(nums2)):\n            if nums2[i]**2 in dict1:\n                ret += dict1[nums2[i]**2]\n        return ret\n            \n            \n", "from collections import Counter\n\nclass Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        n1, n2 = Counter([x*x for x in nums1]), Counter([x*x for x in nums2])\n        count = 0\n        l1, l2 = len(nums1), len(nums2)\n        for i in range(l2-1):\n            for j in range(i+1, l2):\n                count += n1[nums2[i] * nums2[j]]\n        for i in range(l1-1):\n            for j in range(i+1, l1):\n                count += n2[nums1[i] * nums1[j]]\n        \n        return count\n", "from collections import Counter\n\nclass Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        def find_triplets(nums_1, nums_2):\n            ans = 0\n            target = list(map(lambda x: x**2, nums_1))\n            count = Counter(target)\n            for j in range(len(nums_2)):\n                for k in range(j+1, len(nums_2)):\n                    if nums_2[j] * nums_2[k] in count:\n                        ans += count[nums_2[j] * nums_2[k]]\n            return ans\n\n        return find_triplets(nums1, nums2) + find_triplets(nums2, nums1)", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        d1=collections.defaultdict(int)\n        d2=collections.defaultdict(int)\n        for i in range(len(nums1)-1):\n            for j in range(i+1,len(nums1)):\n                d1[nums1[i]*nums1[j]]+=1\n        for i in range(len(nums2)-1):\n            for j in range(i+1,len(nums2)):\n                d2[nums2[i]*nums2[j]]+=1\n        c=0\n        for i in nums1:\n            c+=d2[i*i]\n        for i in nums2:\n            c+=d1[i*i]\n        print(d1,d2)\n        return c", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        \n        def helper(a, b):\n            count = Counter(x ** 2 for x in a)\n            return sum(count[x * y] for x, y in itertools.combinations(b, 2))\n\n        return helper(nums1, nums2) + helper(nums2, nums1)", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        l1 = len(nums1)\n        l2 = len(nums2)\n        upper1 = []\n        upper2 = []\n        for i in range(l1):\n            for j in range(i+1, l1):\n                upper1.append(nums1[i]*nums1[j])\n        for i in range(l2):\n            for j in range(i+1, l2):\n                upper2.append(nums2[i]*nums2[j])\n        Count_upper1 = collections.Counter(upper1)\n        Count_upper2 = collections.Counter(upper2)\n        count = 0\n        for i in range(l1):\n            count += Count_upper2[nums1[i]**2]\n        for i in range(l2):\n            count += Count_upper1[nums2[i]**2]\n        return count", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        target1 = {}\n        target2 = {}\n        \n        for i in range(len(nums1)):\n            for j in range(i+1, len(nums1)):\n                v = nums1[i] * nums1[j]\n                if v in target1:\n                    target1[v] += 1\n                else :\n                    target1[v] = 1\n        \n        for i in range(len(nums2)):\n            for j in range(i+1, len(nums2)):\n                v = nums2[i] * nums2[j]\n                if v in target2:\n                    target2[v] += 1\n                else:\n                    target2[v] = 1\n        \n        answer = 0\n        print(target1, target2)\n        for num in nums1:\n            if num*num in target2:\n                answer += target2[num*num]\n        for num in nums2:\n            if num*num in target1:\n                answer += target1[num*num]\n                \n        return answer", "class Solution:\n    def numTriplets(self, a1: List[int], a2: List[int]) -> int:        \n        ans, m1, m2, n1, n2 = 0, Counter([x**2 for x in a1]), Counter([x**2 for x in a2]), len(a1), len(a2)\n        for i in range(n1-1):\n            for j in range(i+1, n1):\n                ans += m2[a1[i]*a1[j]]\n        for i in range(n2-1):\n            for j in range(i+1, n2):\n                ans += m1[a2[i]*a2[j]]\n        return ans", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        totalcount=0\n        nums1sq = defaultdict(int)\n        for sq_val in [x*x for x in nums1]:\n            nums1sq[sq_val]+=1\n        nums2sq = defaultdict(int)\n        for sq_val in [x*x for x in nums2]:\n            nums2sq[sq_val]+=1\n        #count type1\n        for i in range(len(nums2)):\n            for j in range(i+1,len(nums2)):\n                totalcount+=nums1sq[nums2[i]*nums2[j]]\n        #count type2\n        for i in range(len(nums1)):\n            for j in range(i+1,len(nums1)):\n                totalcount+=nums2sq[nums1[i]*nums1[j]]\n        return totalcount\n                    \n        \n", "from typing import List\nfrom collections import Counter\nclass Solution:\n  def numTriplets(self, en1: List[int], en2: List[int]) -> int:\n\n\n\n\n    counter1=Counter(en1)\n    counter2=Counter(en2)\n\n  \n    ct = 0\n    for ele1,ct1 in list(counter1.items()):\n      tot = ele1*ele1\n      for ele2 in counter2:\n        div,mod = divmod(tot,ele2)\n        if mod !=0: continue\n        if div in counter2:\n          if ele2 == div:\n            ct+=ct1*counter2[ele2]*(counter2[div]-1)\n          else:\n            ct+=ct1*counter2[ele2]*counter2[div]\n            \n    for ele1,ct1 in list(counter2.items()):\n      tot = ele1*ele1\n      for ele2 in counter1:\n        div,mod = divmod(tot,ele2)\n        if mod !=0: continue\n        if div in counter1:\n          if ele2 == div:\n            ct+=ct1*counter1[ele2]*(counter1[div]-1)\n          else:\n            ct+=ct1*counter1[ele2]*counter1[div]\n\n    return ct//2\n", "class Solution:\n    def numTriplets(self, a1: List[int], a2: List[int]) -> int:        \n        ans, m1, m2, n1, n2 = 0, Counter([x**2 for x in a1]), Counter([x**2 for x in a2]), len(a1), len(a2)\n        for i in range(n1-1):\n            for j in range(i+1, n1):\n                ans += m2[a1[i]*a1[j]]\n        for i in range(n2-1):\n            for j in range(i+1, n2):\n                ans += m1[a2[i]*a2[j]]\n        return ans\n\n        \n        \n", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        nums1.sort()\n        nums2.sort()\n        n1 = collections.Counter([num**2 for num in nums1])\n        cnt = 0\n        for i in range(1, len(nums2)):\n            for j in range(i):\n                if nums2[i]*nums2[j] in n1:\n                    cnt += n1[nums2[i]*nums2[j]]\n        \n        n1 = collections.Counter([num**2 for num in nums2])\n        for i in range(1, len(nums1)):\n            for j in range(i):\n                if nums1[i]*nums1[j] in n1:\n                    cnt += n1[nums1[i]*nums1[j]]\n        return cnt", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        counter1 = Counter(num * num for num in nums1)\n        counter2 = Counter(num * num for num in nums2)\n        \n        res = 0\n        \n        n, m = len(nums1), len(nums2)\n        for i in range(n - 1):\n            for j in range(i + 1, n):\n                curr = nums1[i] * nums1[j]\n                res += counter2[curr]\n        \n        for i in range(m - 1):\n            for j in range(i + 1, m):\n                curr = nums2[i] * nums2[j]\n                res += counter1[curr]\n        \n        return res", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        ans, m1, m2 = 0, Counter([x**2 for x in nums1]), Counter([x**2 for x in nums2])\n        n1, n2 = len(nums1), len(nums2)\n        \n        for i in range(n1-1):\n            for j in range(i+1, n1):\n                ans += m2[nums1[i]*nums1[j]]\n        for i in range(n2-1):\n            for j in range(i+1, n2):\n                ans += m1[nums2[i]*nums2[j]]\n        return ans", "#from collections import defaultdict\nfrom collections import  Counter\n\nclass Solution:\n    \n    def check_square(self, c, nums):\n        tot = 0\n        for j in range(len(nums)):\n            for k in range(j+1, len(nums)):\n                tot += c[nums[j] * nums[k]]\n        return tot\n                \n    def count_squares(self, nums):\n        c = Counter([elem**2 for elem in nums])\n        return c\n    \n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        c1 = self.count_squares(nums1)\n        c2 = self.count_squares(nums2)\n        \n        \n        sol1 = self.check_square(c1, nums2)\n        sol2 = self.check_square(c2, nums1)\n        \n        return sol1+sol2", "from collections import Counter\nclass Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        n1_cnts, n2_cnts = Counter(nums1), Counter(nums2)\n        \n        good = 0\n        for n1, cnt in list(n1_cnts.items()):\n            n2_cnts_copy = n2_cnts.copy()\n            for n2j in nums2:\n                n2_cnts_copy[n2j] -= 1\n                good += cnt*n2_cnts_copy[pow(n1, 2)/n2j]\n                \n        for n2, cnt in list(n2_cnts.items()):\n            n1_cnts_copy = n1_cnts.copy()\n            for n1j in nums1:\n                n1_cnts_copy[n1j] -= 1\n                good += cnt*n1_cnts_copy[pow(n2, 2)/n1j]\n                \n        return good\n            \n        \n", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        c1 = collections.Counter([i*i for i in nums1])\n        c2 = collections.Counter([i*i for i in nums2])\n        res = 0\n        for i in range(len(nums1)):\n            for j in range(i+1,len(nums1)):\n                res += c2[nums1[i]*nums1[j]]\n        for i in range(len(nums2)):\n            for j in range(i+1,len(nums2)):\n                res += c1[nums2[i]*nums2[j]]\n        return res", "class Solution:\n    def wrapper(self, toSquare, toProd):\n        \n        #create a hash table of count of unique squared ints in arr 1\n        table = {}\n        for integer in toSquare:\n            squared = integer**2\n            if squared not in table:\n                table[squared] = 1\n            else:\n                table[squared] += 1\n        \n        counter = 0\n        #get all pairs products, check in hash table, if so, inc counter by count in table\n        i = 0\n        while i < len(toProd): #minus one ok too?\n            j = i + 1\n            while j < len(toProd):\n                product = toProd[i] * toProd[j]\n                if product in table:\n                    counter += table[product]\n                j += 1\n            i += 1\n        \n        return counter\n            \n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        \n        counter = 0\n        counter += self.wrapper(nums1, nums2)\n        counter += self.wrapper(nums2, nums1)\n        return counter", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        ans = 0\n        set1 = collections.Counter([x*x for x in nums1])\n        for i in range(len(nums2)):\n            for j in range(i+1, len(nums2)):\n                ans += set1[nums2[i]*nums2[j]]\n        set2 = collections.Counter([x*x for x in nums2])\n        for i in range(len(nums1)):\n            for j in range(i+1, len(nums1)):\n                ans += set2[nums1[i]*nums1[j]]\n                \n        return ans\n        \n", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        import numpy as np\n        l1 = len(nums1)\n        l2 = len(nums2)\n        upper1 = list(np.outer(np.array(nums1), np.array(nums1))[np.triu_indices(l1, 1)])\n        upper2 = list(np.outer(np.array(nums2), np.array(nums2))[np.triu_indices(l2, 1)])\n        Count_upper1 = collections.Counter(upper1)\n        Count_upper2 = collections.Counter(upper2)\n        count = 0\n        for i in range(max(l1,l2)):\n            if i < l1:\n                count += Count_upper2[nums1[i]**2]\n            if i < l2:\n                count += Count_upper1[nums2[i]**2]\n        return count", "from collections import Counter\nclass Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        new1 = Counter(nums1)\n        new2 = Counter(nums2)\n        \n        def helper(a, b):\n            count = 0\n\n            for key in a:\n                target = key ** 2\n\n                for kk in b:\n                    c = target // kk\n\n                    if c < kk:\n                        continue\n                    elif c == kk:\n                        count += b[kk] * (b[kk] - 1) * a[key] // 2\n                    else:\n                        if target % kk == 0 and c in b:\n                            count += b[kk] * b[c] * a[key]\n            \n            return count\n\n        return helper(new1, new2) + helper(new2, new1)", "from collections import Counter\nclass Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        squares1 = Counter([x**2 for x in nums1])\n        squares2 = Counter([x**2 for x in nums2])\n        \n        count = 0\n        for i in range(len(nums1)):\n            for j in range(i+1, len(nums1)):\n                if nums1[i]*nums1[j] in squares2:\n                    count = count + squares2[nums1[i]*nums1[j]]\n        \n        for i in range(len(nums2)):\n            for j in range(i+1, len(nums2)):\n                if nums2[i]*nums2[j] in squares1:\n                    count = count + squares1[nums2[i]*nums2[j]]\n                    \n        return count\n        \n", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        sq_cnt1 = Counter([x*x for x in nums1])\n        sq_cnt2 = Counter([x*x for x in nums2])\n        count = 0\n        for i in range(len(nums1)):\n            for j in range(i+1, len(nums1)):\n                if nums1[i]*nums1[j] in sq_cnt2:\n                    count += sq_cnt2[nums1[i]*nums1[j]]\n                    \n        for i in range(len(nums2)):\n            for j in range(i+1, len(nums2)):\n                if nums2[i]*nums2[j] in sq_cnt1:\n                    count += sq_cnt1[nums2[i]*nums2[j]]\n                    \n        return count", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        def cal(lst1, lst2):\n            count = 0\n            d = {}\n            for i in range(len(lst1)):\n                v = lst1[i]*lst1[i] \n                d[v] = d.get(v, 0)+1\n            for j in range(len(lst2)):\n                for k in range(j+1, len(lst2)):\n                    v = lst2[j]*lst2[k] \n                    if v in d:\n                        count += d[v]\n            return count\n\n        return cal(nums1, nums2) + cal(nums2, nums1)\n", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        N1 = len(nums1)\n        N2 = len(nums2)\n        d1 = collections.defaultdict(int)\n        for j in range(N1):\n            for k in range(j + 1, N1):\n                d1[nums1[j] * nums1[k]] += 1\n        d2 = collections.defaultdict(int)\n        for j in range(N2):\n            for k in range(j + 1, N2):\n                d2[nums2[j] * nums2[k]] += 1\n        ans = 0\n        for i in range(N1):\n            ans += d2[nums1[i] * nums1[i]]\n        for i in range(N2):\n            ans += d1[nums2[i] * nums2[i]]\n        return ans", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        \n\n        c1 = collections.Counter([x**2 for x in nums1])\n        c2 = collections.Counter([y**2 for y in nums2])\n        ans = 0\n        for i in range(len(nums2)):\n            for j in range(i+1, len(nums2)):\n                p = nums2[i]*nums2[j]\n                ans += c1[p]\n        \n        \n        for ii in range(len(nums1)):\n            for jj in range(ii+1, len(nums1)):\n                p = nums1[ii]*nums1[jj]\n                ans += c2[p]\n        \n        return ans\n        \n", "from collections import defaultdict\n\nclass Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        mp1 = defaultdict(int)\n        mp2 = defaultdict(int)\n        for n in nums1:\n            mp1[n*n] += 1\n        for n in nums2:\n            mp2[n*n] += 1\n\n        products1 = self.products(nums1)\n        products2 = self.products(nums2)\n        # print(products1, products2, mp1, mp2)\n        \n        score = 0\n        for p in products1:\n            if mp2.get(p):\n                score += mp2[p]\n        for p in products2:\n            if mp1.get(p):\n                score += mp1[p]\n        \n        return score\n\n    \n    def products(self, nums):\n        products = []\n        n = len(nums)\n        for i in range(0, n-1):\n            for m in nums[i+1:]:\n                products.append(nums[i]*m)\n        return products\n        \n            \n", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        m = len(nums1)\n        n = len(nums2)\n        \n        pairs1 = []\n        pairs2 = []\n        \n        for i in range(n):\n            for j in range(i+1,n):\n                pairs2.append(nums2[i]*nums2[j])\n                \n        for i in range(m):\n            for j in range(i+1,m):\n                pairs1.append(nums1[i]*nums1[j])\n        pairs1.sort()\n        pairs2.sort()\n        if len(pairs1) == 0 or len(pairs2) == 0:\n            return 0\n        # print(pairs1,pairs2)\n        def bs1(arr,key):\n            if arr[0] > key:\n                return -1\n            if arr[-1] < key:\n                return -1\n            lo = 0\n            hi = len(arr)-1\n            left = -1\n            while lo <= hi:\n                mid = (lo+hi)//2\n                if arr[mid] < key:\n                    lo = mid+1\n                elif arr[mid] == key:\n                    left = mid\n                    hi=mid-1\n                else:\n                    hi = mid-1\n            # print('left ',left, key,arr)       \n            return left\n        \n        def bs2(arr,key):\n            if arr[0] > key:\n                return -1\n            if arr[-1] < key:\n                return -1\n            lo = 0\n            hi = len(arr)-1\n            right = -1\n            while lo <= hi:\n                mid = (lo+hi)//2\n                if arr[mid] < key:\n                    lo = mid+1\n                elif arr[mid] == key:\n                    right = mid\n                    lo=mid+1\n                else:\n                    hi = mid-1\n                    \n            return right\n        \n        ans = 0\n        for i in nums1:\n            sq = i*i\n            left = bs1(pairs2,sq)\n            right = bs2(pairs2,sq)\n            if left == -1:\n                continue\n            ans += right-left+1\n            \n        for i in nums2:\n            sq = i*i\n            left = bs1(pairs1,sq)\n            right = bs2(pairs1,sq)\n            if left == -1:\n                continue\n            ans += right-left+1\n        return ans", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        count=0\n        dict1={}\n        dict2={}\n        for i in nums1:\n            x=i*i\n            if x not in dict1.keys():\n                dict1[x]=1\n            else:\n                dict1[x]=dict1[x]+1\n        for j in nums2:\n            x=j*j\n            if x not in dict2.keys():\n                dict2[x]=1\n            else:\n                dict2[x]=dict2[x]+1\n        print(dict1)\n        print(dict2)\n        for j in range(0,len(nums2)):\n            for k in range(j+1,len(nums2)):\n                if nums2[k]*nums2[j] in dict1.keys():\n                    count=count+dict1[nums2[j]*nums2[k]]\n        for j in range(0,len(nums1)):\n            for k in range(j+1,len(nums1)):\n                if nums1[j]*nums1[k] in dict2.keys():\n                    count=count+dict2[nums1[j]*nums1[k]]\n        return count", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        \n        def getRoots(nums):\n            prods = []\n            for (i, j) in itertools.combinations(nums, 2):\n                    prods.append(i*j)\n            return prods\n        \n        def ways(nums1, nums2):\n            ans = 0\n            square = collections.Counter([n**2 for n in nums1])\n            prods = getRoots(nums2)\n            # print(roots)\n            for p in prods:\n                    ans += square[p]\n            return ans\n        \n        return ways(nums1, nums2) + ways(nums2, nums1)\n        \n", "from collections import Counter\nclass Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        new1 = Counter(nums1)\n        new2 = Counter(nums2)\n        \n        def helper(a, b):\n            count = 0\n\n            for key in a:\n                target = key ** 2\n\n                for kk in sorted(b.keys()):\n                    c = target // kk\n\n                    if c < kk:\n                        break\n                    elif c == kk:\n                        count += b[kk] * (b[kk] - 1) * a[key] // 2\n                    else:\n                        if target % kk == 0 and c in b:\n                            count += b[kk] * b[c] * a[key]\n            \n            return count\n\n        return helper(new1, new2) + helper(new2, new1)", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        def typeone(n1,n2):\n            L1=len(n1)\n            L2=len(n2)\n            dic={}\n            count=0\n            for j in range(L2-1):\n                for k in range(j+1,L2):\n                    if n2[j]*n2[k] not in dic:\n                        dic[n2[j]*n2[k]]=1\n                    else:\n                        dic[n2[j]*n2[k]]+=1\n            for u in n1:\n                if u*u in dic:\n                    count+=dic[u*u]\n            return count\n        return typeone(nums1,nums2)+typeone(nums2,nums1)", "from collections import defaultdict\nclass Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        t1 = defaultdict(int)\n        t2 = defaultdict(int)\n        ans = 0\n        for i in range(len(nums1)-1):\n            for j in range(i+1,len(nums1)):\n                t1[nums1[i]*nums1[j]] += 1\n        for i in range(len(nums2)-1):\n            for j in range(i+1,len(nums2)):\n                t2[nums2[i]*nums2[j]] += 1\n                \n        for n in nums1:\n            ans += t2[n*n]\n        for n in nums2:\n            ans += t1[n*n]\n        return ans", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        from math import sqrt\n        from collections import Counter\n        def find(target, pool):\n            ans = 0\n            target = Counter(target)\n            for i in range(0, len(pool)-1):\n                for j in range(i + 1, len(pool)):\n                    ans += target.get(sqrt(pool[i]*pool[j]), 0)\n            return ans\n        \n        return find(nums1, nums2) + find(nums2, nums1)", "def func(xs, ys):\n    sqs = [x**2 for x in xs]\n    seen = collections.defaultdict(int)\n    res = 0\n    for y in ys:\n        for x in sqs:\n            if x % y == 0:\n                res += seen[x//y]\n        seen[y] += 1\n    \n    return res\n\nclass Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        return func(nums1, nums2) + func(nums2, nums1)", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        dic1 = {}\n        dic2 = {}\n        for i in range(len(nums1)-1):\n            for j in range(i+1, len(nums1)):\n                v = nums1[i] * nums1[j]\n                if dic1.get(v) is None:\n                    dic1[v] = 1\n                else:\n                    dic1[v] = dic1[v]+1\n        for i in range(len(nums2)-1):\n            for j in range(i+1, len(nums2)):\n                v = nums2[i] * nums2[j]\n                if dic2.get(v) is None:\n                    dic2[v] = 1\n                else:\n                    dic2[v] = dic2[v]+1\n        cnt = 0\n        for v in nums1:\n            if dic2.get(v*v):\n                cnt += dic2.get(v*v)\n        for v in nums2:\n            if dic1.get(v*v):\n                cnt += dic1.get(v*v)\n        return cnt\n", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        n1=[x*x for x in nums1]\n        n2=[x*x for x in nums2]\n        dnum1=collections.Counter(n1)\n        dnum2=collections.Counter(n2)                \n        ans=0\n        nums1.sort()\n        nums2.sort()\n        m1=max(dnum1.keys())\n        m2=max(dnum2.keys())\n        for i in range(len(nums1)-1):\n            for j in range(i+1,len(nums1)):\n                comb=nums1[i]*nums1[j]\n                if comb in dnum2:\n                    ans+=dnum2[nums1[i]*nums1[j]]\n                if comb>m2:\n                    break\n               \n        for i in range(len(nums2)-1):\n            for j in range(i+1,len(nums2)):\n                comb=nums2[i]*nums2[j]\n                if comb in dnum1:\n                    ans+=dnum1[nums2[i]*nums2[j]]\n                if comb>m1:\n                    break\n        return ans", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        Type1=dict()\n        Product1=dict()\n        Type2=dict()\n        Product2=dict()\n        for i in range(len(nums1)):\n            product= nums1[i]*nums1[i]\n            if product in Product1.keys():\n                Product1[product]+=1\n            else:\n                Product1[product]=1\n            for j in range(i+1, len(nums1)):\n                new_product=nums1[i]*nums1[j]\n                if new_product in Type1.keys():\n                    Type1[new_product]+=1\n                else:\n                    Type1[new_product]=1\n        for i in range(len(nums2)):\n            product= nums2[i]*nums2[i]\n            if product in Product2.keys():\n                Product2[product]+=1\n            else:\n                Product2[product]=1\n            for j in range(i+1, len(nums2)):\n                new_product=nums2[i]*nums2[j]\n                if new_product in Type2.keys():\n                    Type2[new_product]+=1\n                else:\n                    Type2[new_product]=1\n        res=0\n        for i in Product1.keys():\n            if i in Type2.keys():\n                res+=Product1[i]*Type2[i]\n        for i in Product2.keys():\n            if i in Type1.keys():\n                res+=Product2[i]*Type1[i]\n        return res", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        map1={}\n        map2={}\n        for i in range(len(nums1)):\n            for j in range(i+1,len(nums1)):\n                prod=nums1[i]*nums1[j]\n                map1.setdefault(prod,0)\n                map1[prod]+=1\n        for i in range(len(nums2)):\n            for j in range(i+1,len(nums2)):\n                prod=nums2[i]*nums2[j]\n                map2.setdefault(prod,0)\n                map2[prod]+=1\n\n        res=0\n        for num in nums1:\n            if num**2 in map2:\n                res+=map2[num**2]\n        for num in nums2:\n            if num**2 in map1:\n                res+=map1[num**2]\n        return res\n", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        products1 = collections.defaultdict(int)\n        products2 = collections.defaultdict(int)\n        \n        result = 0\n        \n        for i in range(len(nums1)):\n            for j in range(i+1, len(nums1)):\n                p = nums1[i]*nums1[j]\n                products1[p] += 1\n        \n        for i in range(len(nums2)):\n            for j in range(i+1, len(nums2)):\n                p = nums2[i]*nums2[j]\n                products2[p] += 1\n        \n        for num in nums1:\n            result += products2[num*num]\n        \n        for num in nums2:\n            result += products1[num*num]\n        \n        return result\n", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        count = self.findTriple(nums1, nums2)\n        count += self.findTriple(nums2, nums1)\n        \n        return count\n        \n    def findTriple(self, nums1, nums2):\n        counts = {}\n        for num in nums2:\n            if num not in counts:\n                counts[num] = 0\n            counts[num] += 1\n        \n        tcount = 0\n        for i in range(0, len(nums1)):\n            for j in range(0, len(nums2)):\n                if nums2[j] < nums1[i] and (nums1[i] * nums1[i]) % nums2[j] == 0 and (nums1[i] * nums1[i]) // nums2[j] in counts:\n                    tcount += counts[nums1[i] * nums1[i] // nums2[j]]\n        \n            if nums1[i] in counts:\n                tcount += (counts[nums1[i]] * (counts[nums1[i]] - 1) // 2)\n        \n        return tcount\n", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        hash1=defaultdict(int)\n        hash2=defaultdict(int)\n        \n        count =0\n        for i in nums1:\n            hash1[i*i]+=1\n            \n        for i in nums2:\n            hash2[i*i]+=1\n            \n        for i in range(len(nums1)-1):\n            for j in range(i+1,len(nums1)):\n                if(nums1[i]*nums1[j] in hash2):\n                    count+=hash2[nums1[i]*nums1[j]]\n                    \n        for i in range(len(nums2)-1):\n            for j in range(i+1, len(nums2)):\n                if(nums2[i]*nums2[j] in hash1):\n                    count+= hash1[nums2[i]*nums2[j]]\n        \n        return count", "class Solution:\n    def numTriplets(self, a: List[int], b: List[int]) -> int:\n        \n        # i^2 / k == j\n        \n        def count(a, b):\n            \n            c = Counter()\n            \n            for x,y in combinations(b, 2):\n                c[x*y] += 1\n            \n            ret = 0\n            \n            for x in a:\n                ret += c[x*x]    \n            \n            return ret\n        \n        return count(a,b) + count(b,a)\n", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        def twoProd(n1, n2):\n            d = Counter()\n            for i, a in enumerate(n1):\n                for j in range(i+1, len(n1)):\n                    d[a*n1[j]] += 1\n        \n            return sum(d[i*i] for i in n2)\n        \n        return twoProd(nums1, nums2) + twoProd(nums2, nums1)        ", "class Solution:\n    def wrapper(self, toSquare, toProd):\n        \n        #create a hash table of count of unique squared ints in arr 1\n        table = {}\n        for integer in toSquare:\n            squared = integer**2\n            if squared not in table:\n                table[squared] = 1\n            else:\n                table[squared] += 1\n        \n        counter = 0\n        #get all pairs products, check in hash table, if so, inc counter by count in table\n        i = 0\n        while i < len(toProd): #minus one ok too?\n            j = i + 1\n            while j < len(toProd):\n                product = toProd[i] * toProd[j]\n                if product in table:\n                    counter += table[product]\n                j += 1\n            i += 1\n        \n        return counter\n                    \n    def numTriplets1(self, nums1: List[int], nums2: List[int]) -> int:\n        \n        counter = 0\n        counter += self.wrapper(nums1, nums2)\n        counter += self.wrapper(nums2, nums1)\n        return counter\n    \n    def get_tables(self, nums):\n        squares = {}\n        products = {}\n        i = 0\n        while i != len(nums):\n            j = i\n            while j != len(nums):   # O(N^2)\n                product_or_square = nums[i] * nums[j]\n                if i == j:\n                    if product_or_square not in squares:    # O(1)\n                        squares[product_or_square] = 1     # O(1) \n                    else:\n                        squares[product_or_square] += 1     # O(1)\n                else:\n                    if product_or_square not in products:\n                        products[product_or_square] = 1\n                    else:\n                        products[product_or_square] += 1\n                j += 1\n            i += 1\n        \n        return products, squares\n    \n    def get_counts(self, products, squares):\n        # iterate over the smaller set\n        counter = 0\n        if len(products) > len(squares):\n            for square in squares:          # O(N)\n                if square in products:\n                    counter += squares[square] * products[square]\n        else:\n            for product in products:        #O(M)\n                if product in squares:\n                    counter += products[product] * squares[product]\n        \n        return counter\n    \n    def numTriplets2(self, nums1: List[int], nums2: List[int]) -> int:\n        products1, squares1 = self.get_tables(nums1)\n        products2, squares2 = self.get_tables(nums2)\n        counter = 0\n        counter += self.get_counts(products1, squares2)\n        counter += self.get_counts(products2, squares1)\n\n        return counter\n    \n    def get_products(self, nums):\n        table = {}\n        i = 0\n        while i < len(nums) - 1:\n            j = i + 1\n            while j < len(nums):\n                prod = nums[i] * nums[j]\n                if prod not in table:\n                    table[prod] = 1\n                else:\n                    table[prod] += 1\n                j += 1\n            i += 1\n        \n        return table\n    \n    def check_squares_in_prods(self, nums, prods):\n        counter = 0\n        for i in nums:\n            sq = i**2\n            if sq in prods:\n                counter += prods[sq]\n        return counter\n    \n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        \n        prods1 = self.get_products(nums1)\n        prods2 = self.get_products(nums2)\n\n        counter = 0\n        counter += self.check_squares_in_prods(nums1, prods2)\n        counter += self.check_squares_in_prods(nums2, prods1)\n        \n        return counter\n", "class Solution:\n    def helper(self,nums1,nums2):\n        n1=len(nums1)\n        n2=len(nums2)\n        ans=0\n        h1={}\n        for j in range(n1):\n            for k in range(j+1,n1):\n                if nums1[j]*nums1[k] not in h1:\n                    h1[nums1[j]*nums1[k]]=1\n                else:\n                    h1[nums1[j]*nums1[k]]+=1\n                    \n                    \n        l=[i**2 for i in nums2]\n        \n        \n        ans=0\n        for i in l:\n            if i in h1:\n                ans+=h1[i]\n        return ans\n    \n    \n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        \n        return self.helper(nums1,nums2)+self.helper(nums2,nums1)\n", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        \n        \n\n        def multi(arr):\n            D = {}\n            for i in range(len(arr)):\n                for j in range(i+1,len(arr)):\n                    if arr[i] * arr[j] not in D:\n                        D[arr[i] * arr[j]] = 1\n                    else:\n                        D[arr[i] * arr[j]] += 1\n            return D\n                    \n        nums1_multi = multi(nums1)\n        nums2_multi = multi(nums2)\n        res = 0\n        \n        for i in nums1:\n            res += nums2_multi.get(i**2,0)\n            \n        for i in nums2:\n            res += nums1_multi.get(i**2,0)\n\n        \n        return res     \n\n", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        num1_counts = collections.Counter(nums1)\n        num2_counts = collections.Counter(nums2)\n        res = 0\n        \n        def findMatches(d1, d2):\n            res = 0\n            for n1 in d1:\n                square = n1*n1\n                checked = set()\n                for n2 in d2:\n                    if n2 in checked or square%n2: continue\n\n                    k = square//n2\n                    if k == n2:\n                        res += d1[n1]*d2[n2]*(d2[n2]-1)//2\n                    elif k in d2:\n                        res += d1[n1]*d2[n2]*d2[k]\n                        checked.add(k)\n\n                    checked.add(n2)\n            return res\n        \n        return findMatches(num1_counts, num2_counts) + findMatches(num2_counts,num1_counts)\n                    \n", "class Solution:\n    def numTriplets(self, A: List[int], B: List[int]) -> int:\n        \n        countA = defaultdict(int)\n        countB = defaultdict(int)\n        \n        for i in range(len(A)):\n            for j in range(i + 1, len(A)):\n                countA[A[i] * A[j]] += 1\n        \n        for i in range(len(B)):\n            for j in range(i + 1, len(B)):\n                countB[B[i] * B[j]] += 1\n        \n        res = 0\n        for i in range(len(A)):\n            val = A[i] ** 2\n            res += countB[val]\n            \n        for i in range(len(B)):\n            val = B[i] ** 2\n            res += countA[val]\n        \n        return res\n", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        def findTriplet2(arr1,arr2):\n            result = 0\n            m = len(arr2)\n            count = collections.Counter([x**2 for x in arr1])\n            for j in range(m):\n                for k in range(j+1,m):\n                    if arr2[j]*arr2[k] in list(count.keys()):\n                        result += count[arr2[j]*arr2[k]]\n            return result\n        def findTriplet(arr1,arr2):\n            n,m = len(arr1),len(arr2)\n            triplets = 0\n            for i in range(n):\n                for j in range(m):\n                    for k in range(j+1,m):\n                        if arr1[i]**2 == arr2[j]*arr2[k]:\n                            triplets += 1\n            return triplets\n        return findTriplet2(nums1,nums2)+findTriplet2(nums2,nums1)\n        \n        \n        \n    \n    \n", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        nums1.sort()\n        nums2.sort()\n        \n        def numValid(target, nums):\n            start, end = 0, len(nums) - 1\n            ans = 0\n            while start < end:\n                if target * target == nums[start] * nums[end]:\n                    if nums[start] == nums[end]:\n                        n = end - start + 1\n                        ans += n * (n - 1) // 2\n                        break\n                    else:\n                        start += 1\n                        end -= 1\n                        dup1 = dup2 = 1\n                        while start < end and nums[start - 1] == nums[start]:\n                            start += 1\n                            dup1 += 1\n                        start -= 1\n                        while start < end and nums[end] == nums[end + 1]:\n                            end -= 1\n                            dup2 += 1\n                        ans += dup1 * dup2                    \n                elif target * target < nums[start] * nums[end]:\n                    end -= 1\n                elif target * target > nums[start] * nums[end]:\n                    start += 1\n            return ans\n        \n        ans = 0\n        for num1 in nums1:\n            ans += numValid(num1, nums2)\n        for num2 in nums2:\n            ans += numValid(num2, nums1)\n        return ans", "class Solution(object):\n    def numTriplets(self, A, B):\n        from collections import Counter\n        countA = Counter()\n        countB = Counter()\n        for i, x in enumerate(A):\n            for j in range(i+1, len(A)):\n                y = A[j]\n                countA[x * y] += 1\n        for i, x in enumerate(B):\n            for j in range(i+1, len(B)):\n                y = B[j]\n                countB[x * y] += 1\n        ans = 0\n        for i, x in enumerate(A):\n            ans += countB[x * x]\n        for i, x in enumerate(B):\n            ans += countA[x * x]\n        return ans", "class Solution:\n    def numTriplets(self, A: List[int], B: List[int]) -> int:\n        countA = Counter()\n        countB = Counter()\n        \n        for i in range(len(A)):\n            for j in range(i+1,len(A)):\n                countA[ A[i]* A[j] ] +=1\n                \n        for i in range(len(B)):\n            for j in range(i+1,len(B)):\n                countB[ B[i]* B[j] ] +=1\n                \n        ans=0        \n        for x in A:\n            ans += countB[x*x]\n            \n        for x in B:\n            ans += countA[x*x]\n            \n        return(ans)\n", "from collections import Counter\n\nclass Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        sqr_num1 = Counter(x ** 2 for x in nums1)\n        sqr_num2 = Counter(x ** 2 for x in nums2)\n        len_num1, len_num2 = len(nums1), len(nums2)\n        multi_num1, multi_num2 = Counter(), Counter()\n        \n        if len_num1 > 1:\n            for i in range(len_num1 - 1):\n                for j in range(i + 1, len_num1):\n                    multi_num1[nums1[i] * nums1[j]] += 1\n                    \n        if len_num2 > 1:\n            for i in range(len_num2 - 1):\n                for j in range(i + 1, len_num2):\n                    multi_num2[nums2[i] * nums2[j]] += 1\n                    \n        res = 0\n        \n        for n in sqr_num1:\n            if n in multi_num2:\n                res += sqr_num1[n] * multi_num2[n]\n                \n        for n in sqr_num2:\n            if n in multi_num1:\n                res += sqr_num2[n] * multi_num1[n]\n                \n        return res\n                \n        \n", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        \n        def check(A, B):\n            n, res = len(B), 0\n            C = Counter(map(lambda i: i*i, A))\n            \n            for i in range(n):\n                for j in range(i+1, n):\n                    res += C[B[i] * B[j]]\n                    \n            return res\n        \n        return check(nums1, nums2) + check(nums2, nums1)", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        def h(nums1, nums2):\n            n = len(nums1)\n            counter = collections.Counter()\n            for i in range(n):\n                for j in range(i + 1, n):\n                    counter[nums1[i] * nums1[j]] += 1\n            return sum(counter[i**2] for i in nums2)\n        return h(nums1, nums2) + h(nums2, nums1)", "class Solution:\n    def numTriplets(self, A,B):\n        countA = collections.Counter()\n        countB = collections.Counter()\n        for i in range(len(A)):\n            for j in range(i+1,len(A)):\n                countA[A[i]*A[j]] += 1\n        \n        for i in range(len(B)):\n            for j in range(i+1,len(B)):\n                countB[B[i]*B[j]] += 1\n        ans = 0\n        for x in A:\n            ans += countB[x*x]\n        for x in B:\n            ans += countA[x*x]\n        return ans\n        \n        \n", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        \n        def get_sqs(nums):\n            c = collections.Counter()\n            for j in range(len(nums)):\n                for k in range(j+1,len(nums)):\n                    c[nums[j]*nums[k]]+=1\n            return c\n        \n        sq1s = get_sqs(nums1)\n        sq2s = get_sqs(nums2)\n        \n        res = 0\n        \n        for n in nums1:\n            res+=sq2s[n*n]\n        for n in nums2:\n            res+=sq1s[n*n]\n        \n        return res", "class Solution:\n    def build_triplets(self, numbers1, numbers2):\n        ret = 0\n        \n        \n        for number1, occurrences1 in numbers1.items():\n            number1 *= number1\n            checked_numbers = set()\n            \n            \n            for number2, occurrences2 in numbers2.items():\n                if (number2 not in checked_numbers):\n                    required = number1 // number2\n\n\n                    if (required not in checked_numbers and required * number2 == number1\n                        and required in numbers2):\n                        if (required == number2):\n                            ret += occurrences2 * occurrences1 * (occurrences1 - 1) // 2\n                        else:\n                            ret += occurrences2 * numbers2[required] * occurrences1\n\n                        checked_numbers.add(required)\n                        \n        return ret\n        \n        \n        \n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        numbers1 = collections.Counter(nums1)\n        numbers2 = collections.Counter(nums2)\n        return self.build_triplets(numbers1, numbers2) + self.build_triplets(numbers2, numbers1)", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        n = len(nums1)\n        m = len(nums2)\n        nums2_pairs = Counter()\n        nums1_pairs = Counter()\n        \n        for i in range(n):\n            for j in range(i+1, n):\n                nums1_pairs[nums1[i]*nums1[j]] += 1\n        \n        for i in range(m):\n            for j in range(i+1, m):\n                nums2_pairs[nums2[i]*nums2[j]] += 1\n\n        ans = 0\n        for i in range(n):\n            ans += nums2_pairs[nums1[i]*nums1[i]]\n        for i in range(m):\n            ans += nums1_pairs[nums2[i]*nums2[i]]\n        return ans\n", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        def helper(a,b):\n            ans = 0\n            hashm = {}\n            for i in a:\n                if i*i not in hashm:\n                    hashm[i*i] = 1\n                else:\n                    hashm[i*i] = hashm[i*i]+1\n            for i in range(len(b)):\n                for j in range(i+1,len(b)):\n                    if b[i]*b[j] in hashm:\n                        ans = ans+ hashm[b[i]*b[j]]\n            return ans\n        return helper(nums1,nums2) + helper(nums2,nums1)", "from collections import Counter\nclass Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        mul1, mul2 = Counter(), Counter()\n        for i in range(len(nums1)):\n            for j in range(i+1,len(nums1)):\n                mul1[nums1[i]*nums1[j]] += 1\n        \n        for i in range(len(nums2)):\n            for j in range(i+1,len(nums2)):\n                mul2[nums2[i]*nums2[j]] += 1\n        \n        tots = 0\n        for i in range(len(nums1)):\n            tots+= mul2[nums1[i]**2]\n        for i in range(len(nums2)):\n            tots+= mul1[nums2[i]**2]\n        return tots\n            \n", "class Solution:\n    def numTriplets(self, A: List[int], B: List[int]) -> int:\n        def getTriplets(A, B):\n            m, n = len(A), len(B)\n            ans = 0\n            counter = Counter()\n            for j in range(n):\n                for k in range(j+1,n):\n                    prod = B[j]*B[k]\n                    counter[prod] += 1\n            for i in range(m):\n                prod = A[i]**2\n                ans += counter[prod]\n            return ans \n        return getTriplets(A,B) + getTriplets(B,A)", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        rep=0\n        rep+=self.getNumTriplets(nums1,nums2)\n        rep+=self.getNumTriplets(nums2,nums1)\n        return rep\n    \n    def getNumTriplets(self,nums1,nums2):\n        num1=[n**2 for n in nums1]\n        num2=[]\n        for i in range(len(nums2)-1):\n            for j in range(i+1,len(nums2)):\n                num2.append(nums2[i]*nums2[j])\n        num2.sort()\n        num1.sort()\n        \n        rep=0\n        k=0\n        while k<len(num1):\n            cur=self.exist(num2,num1[k])\n            rep+=cur\n            while k+1<len(num1) and num1[k+1]==num1[k]:\n                rep+=cur\n                k+=1\n            k+=1\n        return rep\n    \n    def exist(self,nums,target):\n        l=0\n        r=len(nums)-1\n        while l<=r:\n            mid=l+(r-l)//2\n            if nums[mid]>target:\n                r=mid-1\n            elif nums[mid]<target:\n                l=mid+1\n            else: # nums[mid]==target\n                l=self.leftSearch(nums[:mid+1],target)\n                r=self.rightSearch(nums[mid:],target)+mid\n                return r-l+1\n        return 0\n    \n    def leftSearch(self,nums,target):\n        l,r=0,len(nums)-1\n        while l<=r:\n            mid=l+(r-l)//2\n            if nums[mid]==target:\n                r=mid-1\n            else:\n                l=mid+1\n        return l\n    \n    def rightSearch(self,nums,target):\n        l,r=0,len(nums)-1\n        while l<=r:\n            mid=l+(r-l)//2\n            if nums[mid]==target:\n                l=mid+1\n            else:\n                r=mid-1\n        return r", "class Solution:\n    def numTriplets(self, A: List[int], B: List[int]) -> int:\n        def getTriplets(A, B):\n            m, n = len(A), len(B)\n            ans = 0\n            counter = Counter()\n            for j in range(n):\n                for k in range(j+1,n):\n                    prod = B[j]*B[k]\n                    counter[prod] += 1\n            for i in range(m):\n                prod = A[i]**2\n                ans += counter[prod]\n            return ans \n        return getTriplets(A,B) + getTriplets(B,A)            \n                    \n            \n", "class Solution:\n    def wrapper(self, toSquare, toProd):\n        \n        #create a hash table of count of unique squared ints in arr 1\n        table = {}\n        for integer in toSquare:\n            squared = integer**2\n            if squared not in table:\n                table[squared] = 1\n            else:\n                table[squared] += 1\n        \n        counter = 0\n        #get all pairs products, check in hash table, if so, inc counter by count in table\n        i = 0\n        while i < len(toProd): #minus one ok too?\n            j = i + 1\n            while j < len(toProd):\n                product = toProd[i] * toProd[j]\n                if product in table:\n                    counter += table[product]\n                j += 1\n            i += 1\n        \n        return counter\n                    \n    def numTriplets1(self, nums1: List[int], nums2: List[int]) -> int:\n        \n        counter = 0\n        counter += self.wrapper(nums1, nums2)\n        counter += self.wrapper(nums2, nums1)\n        return counter\n    \n    def get_tables(self, nums):\n        squares = {}\n        products = {}\n        i = 0\n        while i != len(nums):\n            j = i\n            while j != len(nums):\n                product_or_square = nums[i] * nums[j]\n                if i == j:\n                    if product_or_square not in squares:\n                        squares[product_or_square] = 1\n                    else:\n                        squares[product_or_square] += 1\n                else:\n                    if product_or_square not in products:\n                        products[product_or_square] = 1\n                    else:\n                        products[product_or_square] += 1\n                j += 1\n            i += 1\n        \n        return products, squares\n    \n    def get_counts(self, products, squares):\n        # iterate over the smaller set\n        counter = 0\n        if len(products) > len(squares):\n            for square in squares:\n                if square in products:\n                    counter += squares[square] * products[square]\n        else:\n            for product in products:\n                if product in squares:\n                    counter += products[product] * squares[product]\n        \n        return counter\n    \n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        products1, squares1 = self.get_tables(nums1)\n        products2, squares2 = self.get_tables(nums2)\n        counter = 0\n        counter += self.get_counts(products1, squares2)\n        counter += self.get_counts(products2, squares1)\n\n        return counter\n", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        from collections import Counter\n        m, n = len(nums1), len(nums2)\n        res = 0\n\n        pool2 = Counter()\n        for i in range(n - 1):\n            for j in range(i + 1, n):\n                curr = nums2[i] * nums2[j]\n                pool2[curr] += 1\n\n        for v in nums1:\n            curr = v ** 2\n            if curr in pool2:\n                res += pool2[curr]\n\n        pool1 = Counter()\n        for i in range(m - 1):\n            for j in range(i + 1, m):\n                curr = nums1[i] * nums1[j]\n                pool1[curr] += 1\n\n        for v in nums2:\n            curr = v ** 2\n            if curr in pool1:\n                res += pool1[curr]\n\n        return res", "def func(xs, ys):\n    sqs = [x**2 for x in xs]\n    seen = collections.defaultdict(int)\n    res = 0\n    for y in ys:\n        for x in sqs:\n            res += seen[x/y]\n        seen[y] += 1\n    \n    return res\n\nclass Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        return func(nums1, nums2) + func(nums2, nums1)", "from collections import Counter\n\nclass Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        def helper(A, B):\n            ans = 0\n            C = Counter([a*a for a in A])\n            D = Counter()\n            for b in B:\n                for k, v in D.items():\n                    if k*b in C:\n                        ans += v * C[k*b]\n                D[b] += 1\n            return ans\n        \n        return helper(nums1, nums2) + helper(nums2, nums1)", "import math\nclass Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        cnt=0\n        d={}\n        ans=0\n        for i in range(0,len(nums2)):\n            for j in range(i+1,len(nums2)):\n                if(i!=j):\n                    p=(nums2[i]*nums2[j])\n                    try:\n                        d[p]+=1\n                    except:\n                        d[p]=1\n        #print(d)\n        for i in range(0,len(nums1)):\n            p=nums1[i]*nums1[i]\n            try:\n                if(d[p]>0):\n                    cnt=cnt+d[p]\n            except:\n                ans=1\n        d={}\n        for i in range(0,len(nums1)):\n            for j in range(i+1,len(nums1)):\n                if(i!=j):\n                    p=(nums1[i]*nums1[j])\n                    try:\n                        d[p]+=1\n                    except:\n                        d[p]=1\n        for i in range(0,len(nums2)):\n            p=nums2[i]*nums2[i]\n            try:\n                if(d[p]>0):\n                    cnt=cnt+d[p]\n            except:\n                ans=1\n        return cnt\n                \n        \n                        \n", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        # (i, j, k) if nums1[i]2 == nums2[j] * nums2[k] \n        # (i, j, k) if nums2[i]2 == nums1[j] * nums1[k] \n    \n        n = len(nums1)\n        m = len(nums2)\n        n2dict = defaultdict(int)\n        m2dict = defaultdict(int)\n        for x in nums1:\n            n2dict[x*x]+=1\n        for x in nums2:\n            m2dict[x*x]+=1\n        # n2 = [x*x for i,x in enumerate(nums1)]\n        # n2c= Counter(n2)\n        # m2 = [x*x for i,x in enumerate(nums2)]\n        # m2c= Counter(m2)\n        out = 0\n        \n       #print(n2, m2)\n        #print(n2c, m2c)\n            \n        for i in range(m):\n            for j in range(i+1, m):\n                key = nums2[i] * nums2[j]\n                #print(key)\n                if nums2[i] * nums2[j] in n2dict:\n                    out += (n2dict[key])\n                    #print(key, nums2[i], nums2[j])\n                    \n        #print()\n        for i in range(n):\n            for j in range(i+1, n):\n                key = nums1[i] * nums1[j]\n                \n                #print(key)\n                if key in m2dict:\n                    out += (m2dict[key])\n                    #print(key, nums1[i], nums1[j])\n                    \n        return out\n        \n        \n", "from collections import defaultdict\nclass Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        h1=defaultdict(list)\n        h2=defaultdict(list)\n        n=len(nums1)\n        m=len(nums2)\n        ans=0\n        for i in range(n):\n            for j in range(i+1,n):\n                h1[nums1[i]*nums1[j]].append((i,j))\n        for i in range(m):\n            for j in range(i+1,m):\n                h2[nums2[i]*nums2[j]].append((i,j))\n        for i in range(n):\n            if nums1[i]**2 in h2:\n                ans+=len(h2[nums1[i]**2])\n        for i in range(m):\n            if nums2[i]**2 in h1:\n                ans+=len(h1[nums2[i]**2])\n        return ans\n        \n                \n", "class Solution:\n    def wrapper(self, toSquare, toProd):\n        \n        #create a hash table of count of unique squared ints in arr 1\n        table = {}\n        for integer in toSquare:\n            squared = integer**2\n            if squared not in table:\n                table[squared] = 1\n            else:\n                table[squared] += 1\n        \n        counter = 0\n        #get all pairs products, check in hash table, if so, inc counter by count in table\n        i = 0\n        while i < len(toProd): #minus one ok too?\n            j = i + 1\n            while j < len(toProd):\n                product = toProd[i] * toProd[j]\n                if product in table:\n                    counter += table[product]\n                j += 1\n            i += 1\n        \n        return counter\n                    \n    def numTriplets1(self, nums1: List[int], nums2: List[int]) -> int:\n        \n        counter = 0\n        counter += self.wrapper(nums1, nums2)\n        counter += self.wrapper(nums2, nums1)\n        return counter\n    \n    def get_tables(self, nums):\n        squares = {}\n        products = {}\n        i = 0\n        while i != len(nums):\n            j = i\n            while j != len(nums):\n                product_or_square = nums[i] * nums[j]\n                if i == j:\n                    if product_or_square not in squares:\n                        squares[product_or_square] = 1\n                    else:\n                        squares[product_or_square] += 1\n                else:\n                    if product_or_square not in products:\n                        products[product_or_square] = 1\n                    else:\n                        products[product_or_square] += 1\n                j += 1\n            i += 1\n        \n        return products, squares\n    \n    def get_counts(self, products, squares):\n        # iterate over the smaller set\n        counter = 0\n        if len(products) > len(squares):\n            for square in list(squares.keys()):\n                if square in products:\n                    counter += squares[square] * products[square]\n        else:\n            for product in list(products.keys()):\n                if product in squares:\n                    counter += products[product] * squares[product]\n        \n        return counter\n    \n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        products1, squares1 = self.get_tables(nums1)\n        products2, squares2 = self.get_tables(nums2)\n        counter = 0\n        counter += self.get_counts(products1, squares2)\n        counter += self.get_counts(products2, squares1)\n\n        return counter\n", "from collections import Counter\n\nclass Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        output = 0\n        sq1 = Counter(map(lambda q: q*q, nums1))\n        sq2 = Counter(map(lambda q: q*q, nums2))\n        for i in range(len(nums2) - 1):\n            for j in range(i + 1, len(nums2)):\n                output += sq1[nums2[i]*nums2[j]]\n \n        for i in range(len(nums1) - 1):\n            for j in range(i + 1, len(nums1)):\n                output += sq2[nums1[i]*nums1[j]]\n                \n        return output", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        d1, d2 = dict(), dict()\n\n        for idx, n1 in enumerate(nums1):\n            d1[idx] = dict()\n            for elm in nums1[idx+1:]:\n                try:\n                    d1[idx][elm] += 1\n                except KeyError:\n                    d1[idx][elm] = 1\n\n        for idx, n2 in enumerate(nums2):\n            d2[idx] = dict()\n            for elm in nums2[idx+1:]:\n                try:\n                    d2[idx][elm] += 1\n                except KeyError:\n                    d2[idx][elm] = 1\n                    \n        \n        good_dict = {}\n        for n1 in nums1:\n            if (1, n1) not in good_dict:\n                good = 0\n                for idx, n2 in enumerate(nums2):\n                    try:\n                        good += d2[idx][n1**2/n2]\n                    except KeyError:\n                        pass\n                good_dict[(1, n1)] = [good, 1]\n            else:\n                good_dict[(1, n1)][-1] += 1\n                \n        for n2 in nums2:\n            if (2, n2) not in good_dict:\n                good = 0\n                for idx, n1 in enumerate(nums1):\n                    try:\n                        good += d1[idx][n2**2/n1] \n                    except KeyError:\n                        pass\n                good_dict[(2, n2)] = [good, 1]\n            else:\n                good_dict[(2, n2)][-1] += 1\n        \n        return sum(val*q for val, q in list(good_dict.values()))\n        \n", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        def fun(arr):\n            res={}\n            for i in range(len(arr)-1):\n                for j in range(i+1,len(arr)):\n                    if arr[i]*arr[j] in res.keys():\n                        res[arr[i]*arr[j]] +=1\n                    else:\n                        res[arr[i]*arr[j]] =1\n            return res\n        \n        x= fun(nums1)\n        y= fun(nums2)\n        res = 0\n        for i in range(len(nums1)):\n            if nums1[i]**2 in y.keys():\n                print(nums1[i])\n                res+=y[nums1[i]**2]\n        \n        for i in range(len(nums2)):\n            if nums2[i]**2 in x.keys():\n                res+=x[nums2[i]**2]\n                       \n        return res", "import itertools\nclass Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        dicA={}\n        dicB={}\n        for i in range(len(nums1)):\n            for j in range(i+1,len(nums1)):\n                if nums1[i]*nums1[j] in dicA:\n                    dicA[nums1[i]*nums1[j]]+=1\n                else:\n                    dicA[nums1[i]*nums1[j]]=1\n        for i in range(len(nums2)):\n            for j in range(i+1,len(nums2)):\n                if nums2[i]*nums2[j] in dicB:\n                    dicB[nums2[i]*nums2[j]]+=1\n                else:\n                    dicB[nums2[i]*nums2[j]]=1\n        ans=0\n        for i in range(len(nums1)):\n            try:\n                val=dicB[nums1[i]*nums1[i]]\n                ans+=val\n            except KeyError:\n                pass\n        for i in range(len(nums2)):\n            try:\n                val=dicA[nums2[i]*nums2[i]]\n                ans+=val\n            except KeyError:\n                pass\n        return ans\n            \n\n        \n            \n                    \n", "from collections import Counter\nclass Solution:\n    def numTriplets(self, A: List[int], B: List[int]) -> int:\n        def getTriplets(A, B):\n            m, n = len(A), len(B)\n            ans = 0\n            counter = Counter()\n            for j in range(n):\n                for k in range(j+1,n):\n                    prod = B[j]*B[k]\n                    counter[prod] += 1\n            for i in range(m):\n                prod = A[i]**2\n                ans += counter[prod]\n            return ans \n        return getTriplets(A,B) + getTriplets(B,A)", "import collections\n\nclass Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        counts1 = collections.Counter(nums1)\n        counts2 = collections.Counter(nums2)\n        print(counts1)\n        print(counts2)\n        \n        def doit(c1,c2):\n            total = 0\n            for k1 in c1:\n                for k2 in c2:\n                    neededK = (k1 * k1) / k2\n                    if neededK == int(neededK):\n                        neededK = int(neededK)\n                        if k2 == neededK:\n                            i = c2[neededK]\n                            j = (i-1 + 1) * (i-1) // 2\n                            total += c1[k1] * j \n                        elif k2 < neededK:\n                            total += c1[k1]*c2[k2]*c2[neededK]\n                        print(k1,k2,neededK,total)\n            return total\n        \n        return doit(counts1,counts2) + doit(counts2,counts1)", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        lookup1, lookup2, res = {}, {}, 0\n        for i in range(len(nums1)-1):\n            for j in range(i+1, len(nums1)):\n                if nums1[i]*nums1[j] in lookup1:\n                    lookup1[nums1[i]*nums1[j]] += 1\n                else:\n                    lookup1[nums1[i]*nums1[j]] = 1\n        for i in range(len(nums2)-1):\n            for j in range(i+1, len(nums2)):\n                if nums2[i]*nums2[j] in lookup2:\n                    lookup2[nums2[i]*nums2[j]] += 1\n                else:\n                    lookup2[nums2[i]*nums2[j]] = 1\n        \n        for i in nums1:\n            if i*i in lookup2:\n                res += lookup2[i*i]\n        \n        for i in nums2:\n            if i*i in lookup1:\n                res += lookup1[i*i]\n        return res", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        ans = 0\n        n1 = len(nums1)\n        n2 = len(nums2)\n        \n        nums1_product = defaultdict(int)\n        nums2_product = defaultdict(int)\n        \n        # count pairs for product\n        for i in range(n1-1):\n            for j in range(i+1, n1):\n                nums1_product[(nums1[i] * nums1[j])] += 1\n        \n        for i in range(n2-1):\n            for j in range(i+1, n2):\n                nums2_product[(nums2[i] * nums2[j])] += 1\n        \n        for num1 in nums1:\n            target = num1 ** 2\n            \n            ans += nums2_product[target]\n        \n        for num2 in nums2:\n            target = num2 ** 2\n            \n            ans += nums1_product[target]\n  \n        return ans", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        lookup1, lookup2 = collections.defaultdict(int), collections.defaultdict(int)\n        for n in nums1:\n            lookup1[n] += 1\n        for n in nums2:\n            lookup2[n] += 1\n        res = 0\n        for k in [0, 1]:\n            nums = nums1 if not k else nums2\n            lookup = lookup1 if k else lookup2\n            for i in range(len(nums)):\n                for j in range(i + 1, len(nums)):\n                    prod = nums[i] * nums[j]\n                    k = int(math.sqrt(prod))\n                    if k * k == prod and k in lookup:\n                        res += lookup[k]\n        return res", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        ct=0\n        d={}\n        for j in range(len(nums2)):\n            for k in range(j+1,len(nums2)):\n                if nums2[j]*nums2[k] in d:\n                    d[nums2[j]*nums2[k]]=d[nums2[j]*nums2[k]]+1\n                else:\n                    d[nums2[j]*nums2[k]]=1\n        dd={}\n        for j in range(len(nums1)):\n            for k in range(j+1,len(nums1)):\n                if nums1[j]*nums1[k] in dd:\n                    dd[nums1[j]*nums1[k]]=dd[nums1[j]*nums1[k]]+1\n                else:\n                    dd[nums1[j]*nums1[k]]=1\n        \n        ct=0       \n        for i in range(len(nums1)):\n            x=nums1[i]*nums1[i]\n            if x in d:\n                ct=ct+d[x]\n            \n        for i in range(len(nums2)):\n            x=nums2[i]*nums2[i]\n            if x in dd:\n                ct=ct+dd[x]\n        return ct\n", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        m1_square,m2_square={},{}\n        for x in nums1:\n            x_square = x**2\n            m1_square[x_square]=m1_square.get(x_square,0)+1\n            \n        for x in nums2:\n            x_square=x**2\n            m2_square[x_square]=m2_square.get(x_square,0)+1\n        \n        m,n=len(nums1),len(nums2)\n        ans = 0\n        for j in range(m-1):\n            for k in range(j+1,m):\n                if (nums1[j]*nums1[k]) in m2_square:\n                    ans+=m2_square[nums1[j]*nums1[k]]\n        for j in range(n-1):\n            for k in range(j+1, n):\n                if (nums2[j]*nums2[k]) in m1_square:\n                    ans+=m1_square[nums2[j]*nums2[k]]\n        return ans", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        m1 = collections.defaultdict(int)\n        m2 = collections.defaultdict(int)\n        for i, num in enumerate(nums1):\n            m1[num] += 1\n        for i, num in enumerate(nums2):\n            m2[num] += 1            \n        self.ans = 0\n        \n        def count(nums : List[int], m: dict) -> None:\n            for i in range(len(nums)):\n                for j in range(i + 1, len(nums)):\n                    prod = nums[i] * nums[j]\n                    sqrt = math.sqrt(prod)\n                    if sqrt in m:\n                        self.ans += m[sqrt]\n        count(nums1, m2)\n        count(nums2, m1)\n        return self.ans\n", "import collections\nclass Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        numsproddict=collections.defaultdict(int)\n        for i in range(len(nums1)):\n            for j in range(i+1,len(nums1)):\n                numsproddict[nums1[i]*nums1[j]]+=1\n        numsproddict2=collections.defaultdict(int)\n        for i in range(len(nums2)):\n            for j in range(i+1,len(nums2)):\n                numsproddict2[nums2[i]*nums2[j]]+=1\n        ans=0\n        for i in range(len(nums1)):\n            ans+=numsproddict2[nums1[i]*nums1[i]]\n        for i in range(len(nums2)):\n            ans+=numsproddict[nums2[i]*nums2[i]]\n        return ans", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        squares1 = Counter()\n        squares2 = Counter()\n        for num in nums1:\n            squares1[num*num] += 1\n        for num in nums2:\n            squares2[num*num] += 1\n        \n        numTriplets = 0\n        for i in range(len(nums1)):\n            for j in range(i+1, len(nums1)):\n                if nums1[i] * nums1[j] in squares2:\n                    numTriplets += squares2[nums1[i] * nums1[j]]\n        for i in range(len(nums2)):\n            for j in range(i+1, len(nums2)):\n                if nums2[i] * nums2[j] in squares1:\n                    numTriplets += squares1[nums2[i] * nums2[j]]\n        return numTriplets", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        from math import sqrt\n        from collections import Counter\n        def find(target, pool):\n            ans = 0\n            target = Counter(target)\n            for i in range(0, len(pool)-1):\n                for j in range(i + 1, len(pool)):\n                    # print(sqrt(pool[i]*pool[j]))\n                    if sqrt(pool[i]*pool[j]) in target:\n                        ans += target[sqrt(pool[i]*pool[j])]\n            return ans\n        \n        return find(nums1, nums2) + find(nums2, nums1)", "from collections import defaultdict\nclass Solution:\n    \n    def getNumOfMatch(self,square,nums):\n        res = 0\n        h = defaultdict(int)\n        n = len(nums)\n        for v in nums:\n            if square % v == 0:\n                res += h[square//v]\n                h[v] += 1\n        return res\n        \n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        res = 0\n        \n        for num in nums1:\n            res += self.getNumOfMatch(num*num,nums2)\n        for num in nums2:\n            res += self.getNumOfMatch(num*num,nums1)\n        \n        return res", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        from math import sqrt\n        from collections import Counter\n        def find(target, pool):\n            ans = 0\n            target = Counter(target)\n            for i in range(0, len(pool)-1):\n                for j in range(i + 1, len(pool)):\n                    if sqrt(pool[i]*pool[j]) in target:\n                        ans += target[sqrt(pool[i]*pool[j])]\n            return ans\n        \n        return find(nums1, nums2) + find(nums2, nums1)", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        d1={}\n        d2={}\n        cnt1,cnt2=0,0\n        for i in range(len(nums1)):\n            for j in range(i+1,len(nums1)):\n                if nums1[i]*nums1[j] not in d1:\n                    d1[nums1[i]*nums1[j]]=1\n                else:\n                    d1[nums1[i]*nums1[j]]+=1\n        for i in range(len(nums2)):\n            for j in range(i+1,len(nums2)):\n                if  nums2[i]*nums2[j] not in d2:\n                    d2[nums2[i]*nums2[j]]=1\n                else:\n                    d2[nums2[i]*nums2[j]]+=1\n        for i in range(len(nums1)):\n            if nums1[i]**2 in d2:\n                cnt1+=d2[nums1[i]**2]\n        for i in range(len(nums2)):\n            if nums2[i]**2 in d1:\n                cnt2+=d1[nums2[i]**2]\n        return cnt1+cnt2", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        result = 0\n        sums = {}\n        sums2 = {}\n        for i in range(len(nums2)):\n            for j in range(i+1, len(nums2)):\n                if nums2[i]*nums2[j] in sums:\n                    sums[nums2[i]*nums2[j]] += 1\n                else:\n                    sums[nums2[i]*nums2[j]] = 1\n        for i in nums1:\n            if i**2 in sums:\n                result += sums[i**2]\n        for i in range(len(nums1)):\n            for j in range(i+1, len(nums1)):\n                if nums1[i]*nums1[j] in sums2:\n                    sums2[nums1[i]*nums1[j]] += 1\n                else:\n                    sums2[nums1[i]*nums1[j]] = 1\n        for i in nums2:\n            if i**2 in sums2:\n                result += sums2[i**2]\n        return result", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        from collections import Counter\n        \n        c1 = Counter()\n        c2 = Counter()\n        r1 = 0\n        r2 = 0\n        for i in range(len(nums1)):\n            for j in range(len(nums1)):\n                if i<j:\n                    c1[nums1[i]*nums1[j]]+= 1\n                    \n        for i in range(len(nums2)):\n            for j in range(len(nums2)):\n                if i<j:\n                    c2[nums2[i]*nums2[j]]+= 1\n                    \n        for i in nums2:\n            r1 += c1[i*i]\n        for i in nums1:\n            r2+= c2[i*i]\n        \n        return r1+r2", "import collections\nclass Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        count = 0\n        square1 = [e ** 2 for e in nums1]\n        target1 = collections.Counter(square1)\n        for i in range(len(nums2) - 1):\n            for j in range(i+1, len(nums2)):\n                if nums2[i]*nums2[j] in target1:\n                    count += target1[nums2[i]*nums2[j]]\n        square2 = [e ** 2 for e in nums2]\n        target2 = collections.Counter(square2)\n        for i in range(len(nums1) - 1):\n            for j in range(i+1, len(nums1)):\n                if nums1[i]*nums1[j] in target2:\n                    count += target2[nums1[i]*nums1[j]]\n        return count", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        \n        rev, n1, n2 = 0, Counter(nums1), Counter(nums2)\n        \n        for i in range(len(nums1) - 1):\n            for j in range(i + 1, len(nums1)):\n                t = (nums1[i] * nums1[j]) ** (1/2)\n                \n                if t == int(t) and t in n2:\n                    rev += n2[t]\n        \n        for i in range(len(nums2) - 1):\n            for j in range(i + 1, len(nums2)):\n                t = (nums2[i] * nums2[j]) ** (1/2)\n                \n                if t == int(t) and t in n1:\n                    rev += n1[t]\n        \n        return rev", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        hh=defaultdict(int)\n        nums1.sort()\n        for m,i in enumerate(nums1):\n            hh[i*i]+=1\n        h=defaultdict(int)\n        for i in nums2:\n            h[i]+=1\n        r=0\n        print(h)\n        for i in h:\n            for j in h:\n                if i<=j and i*j in hh:\n                    if i==j:\n                        r+=h[i]*(h[i]-1)//2*hh[i*j]\n                    else:\n                        r+=h[i]*h[j]*hh[i*j]\n        nums1,nums2=nums2,nums1\n        hh=defaultdict(int)\n        nums1.sort()\n        for m,i in enumerate(nums1):\n            hh[i*i]+=1\n        h=defaultdict(int)\n        for i in nums2:\n            h[i]+=1\n        print(h)\n        for i in h:\n            for j in h:\n                if i<=j and i*j in hh:\n                    if i==j:\n                        r+=h[i]*(h[i]-1)//2*hh[i*j]\n                    else:\n                        r+=h[i]*h[j]*hh[i*j]\n        return r", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        res = 0\n        double1, double2 = collections.Counter(), collections.Counter()\n        for n in nums1:\n            double1[n * n] += 1\n        for n in nums2:\n            double2[n * n] += 1\n        for i in range(len(nums1)):\n            for j in range(i + 1, len(nums1)):\n                if nums1[i] * nums1[j] in double2:\n                    res += double2[nums1[i] * nums1[j]]\n        for i in range(len(nums2)):\n            for j in range(i + 1, len(nums2)):\n                if nums2[i] * nums2[j] in double1:\n                    res += double1[nums2[i] * nums2[j]]\n        return res\n", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        dic1, dic2 = collections.Counter(nums1), collections.Counter(nums2)\n        ans = 0\n        m, n = len(nums1), len(nums2)\n        for i in range(m):\n            for j in range(i + 1, m):\n                target = nums1[i] * nums1[j]\n                sq = math.sqrt(target)\n                if sq * sq == target:\n                    ans += dic2[sq]\n        for i in range(n):\n            for j in range(i + 1, n):\n                target = nums2[i] * nums2[j]\n                sq = math.sqrt(target)\n                if sq * sq == target:\n                    ans += dic1[sq]\n        \n        return ans", "from collections import Counter\nclass Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int: \n        mults = lambda arr, N: Counter([arr[i]*arr[j] for i in range(N) for j in range(N) if i != j])\n        squares = lambda arr: list([x**2 for x in arr])\n        \n        count = 0\n        A, B = squares(nums1), mults(nums2, len(nums2))\n        C, D = squares(nums2), mults(nums1, len(nums1))\n        for a in A:\n            if a in B:\n                count += B[a]\n        \n        for a in C:\n            if a in D:\n                count += D[a]\n                \n        return count // 2\n", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        m1, m2 = collections.Counter(), collections.Counter()\n        \n        result = 0\n        \n        for i in range(len(nums1) - 1):\n            for j in range(i + 1, len(nums1)):\n                n = sqrt(nums1[i] * nums1[j])\n                if n % 1 == 0:\n                    m1[n] += 1\n                \n        for i in range(len(nums2) - 1):\n            for j in range(i + 1, len(nums2)):\n                n = sqrt(nums2[i] * nums2[j])\n                if n % 1 == 0:\n                    m2[n] += 1\n        \n        for num in nums1:\n            result += m2[num]\n        for num in nums2:\n            result += m1[num]\n        return result", "from collections import Counter\n\nclass Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        squares1 = Counter([x ** 2 for x in nums1])\n        squares2 = Counter([x ** 2 for x in nums2])\n        total = 0\n        \n        # type 1\n        for i in range(len(nums2)):\n            for j in range(i + 1, len(nums2)):\n                if (square := nums2[i] * nums2[j]) in squares1:\n                    total += squares1[square]\n                    \n        # type 2\n        for i in range(len(nums1)):\n            for j in range(i + 1, len(nums1)):\n                if (square := nums1[i] * nums1[j]) in squares2:\n                    total += squares2[square]\n        \n        return total", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        m = len(nums1)\n        n = len(nums2)\n        \n        \n        mat2 = [[0 for i in range(n)] for i in range(n)] \n        def fillUpper(m,nums):\n            mat = [[0 for i in range(m)] for i in range(m)]\n            mapp1 = defaultdict(int)\n            for i in range(m-1):\n                for j in range(i+1,len(mat)):\n                    # mat[i][j] = nums[i]**2 + nums[j]**2\n                    mapp1[nums[i]*nums[j]]+=1\n                    \n            return  mapp1\n        mat1 = fillUpper(m,nums1)\n        mat2 = fillUpper(n,nums2)\n        res = 0\n        for i in nums1:\n            c=i**2 \n            if(c in mat2):\n                res+=mat2[c]\n        for i in nums2:\n            c= i**2 \n            if(c in mat1):\n                res+=mat1[c]       \n        return res\n", "from collections import defaultdict\nclass Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        product_set1, product_set2 = defaultdict(int), defaultdict(int)\n        numss = [nums1, nums2]\n        for n in range(2):\n            if n == 0:\n                s = product_set1\n                # print(\\\"!\\\")\n            else:\n                s = product_set2\n                # print(\\\"?\\\")\n            nums = numss[n]\n            for i in range(len(nums)):\n                for j in range(i + 1, len(nums)):\n                    s[nums[i] * nums[j]] += 1\n                    \n        ans = 0\n        # print(product_set1, product_set2)\n        for n in range(2):\n            if n == 0:\n                s = product_set2\n            else:\n                s = product_set1\n            nums = numss[n]\n            for i in range(len(nums)):\n                ans += s[nums[i] * nums[i]]\n        return ans", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        total = 0\n        \n        nums1Sum = {}\n        for i in range(len(nums1)):\n            for j in range(i + 1, len(nums1)):\n                nums1Sum[nums1[i] * nums1[j]] = nums1Sum.get(nums1[i] * nums1[j], 0) + 1\n        \n        nums2Sum = {}\n        for i in range(len(nums2)):\n            for j in range(i + 1, len(nums2)):\n                nums2Sum[nums2[i] * nums2[j]] = nums2Sum.get(nums2[i] * nums2[j], 0) + 1\n                \n        for num in nums1:\n            if num * num in nums2Sum:\n                total += nums2Sum[num * num]\n        \n        for num in nums2:\n            if num * num in nums1Sum:\n                total += nums1Sum[num * num]\n        \n        return total", "class Solution:\n    def numTripletsHelper(self, nums1: List[int], nums2: List[int]) -> int:\n        nums1_map = {}\n        count = 0\n\n        for i in nums1:\n            if i in nums1_map:\n                nums1_map[i] += 1\n            else:\n                nums1_map[i] = 1\n        \n        for i in nums2:\n            i_2 = i * i\n            seen = set({})\n            for j in nums1:\n                left = nums1_map[j]\n                if i_2 / j == j:\n                    if (j, j) not in seen and left > 1:\n                        count += int((left * (left - 1)) / 2)\n                        seen.add((j, j))\n                elif i_2 / j in nums1_map and (int(i_2 / j), j) not in seen:\n                    right = nums1_map[i_2 / j]\n                    seen.add((int(i_2 / j), j))\n                    seen.add((j, int(i_2 / j)))\n                    count += left * right\n        \n        return count\n        \n    \n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        return self.numTripletsHelper(nums1, nums2) + self.numTripletsHelper(nums2, nums1)", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        from collections import defaultdict\n        import math\n        n=len(nums1)\n        m=len(nums2)\n        dic1,dic2=defaultdict(int),defaultdict(int)\n        for i in range(n):\n            dic1[nums1[i]]+=1\n        for i in range(m):\n            dic2[nums2[i]]+=1\n        ans=0\n        for i in range(n):\n            for j in range(i+1,n):\n                x=nums1[i]*nums1[j]\n                y=math.sqrt(x)\n                if y%1==0:\n                    # print(y,'y','nunms1',i,j)\n                    ans+=dic2[y]\n        for i in range(m):\n            for j in range(i+1,m):\n                x=nums2[i]*nums2[j]\n                y=math.sqrt(x)\n                if y%1==0:\n                    # print(y,'y','nunms2')\n                    ans+=dic1[y]\n        return ans", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        \n        def check(l1, l2):\n            res = {}\n            j = 0\n            while j < len(l2):\n                k = j+1\n                while k < len(l2):\n                    res[l2[j] * l2[k]] = res.get(l2[j] * l2[k], 0) + 1\n                    k += 1\n                j += 1\n            \n            \n            ans = 0\n            while l1:\n                node = l1.pop()\n                if node in res:\n                    ans += res[node]\n            return ans\n\n            \n        squares1 = [x*x for x in nums1]\n        squares2 = [x*x for x in nums2]\n        return check(squares1, nums2) + check(squares2, nums1)", "from collections import Counter\nfrom typing import List\n\n\nclass Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        res = 0\n\n        l1 = len(nums1)\n        l2 = len(nums2)\n        c1 = Counter()\n        c2 = Counter()\n\n        for j in range(l1):\n            for k in range(j + 1, l1):\n                c1[nums1[j] * nums1[k]] += 1\n\n        for j in range(l2):\n            for k in range(j + 1, l2):\n                c2[nums2[j] * nums2[k]] += 1\n\n        for i in range(l1):\n            res += c2[nums1[i] * nums1[i]]\n\n        for i in range(l2):\n            res += c1[nums2[i] * nums2[i]]\n\n        return res\n", "class Solution:\n    def numTriplets(self, num1: List[int], num2: List[int]) -> int:\n        dic1={}\n        for i in range(len(num1)):\n            for j in range(i+1,len(num1)):\n                if num1[i]*num1[j] in dic1:\n                    dic1[num1[i]*num1[j]]+=1\n                else:\n                    dic1[num1[i]*num1[j]]=1\n        dic2={}\n        for i in range(len(num2)):\n            for j in range(i+1,len(num2)):\n                if num2[i]*num2[j] in dic2:\n                    dic2[num2[i]*num2[j]]+=1\n                else:\n                    dic2[num2[i]*num2[j]]=1\n        ans=0\n        for i in num1:\n            if i*i in dic2:\n                ans+=dic2[i*i]\n        for i in num2:\n            if i*i in dic1:\n                ans+=dic1[i*i]\n        return ans\n", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        def find(A, target):\n            # print(A, target)\n            l, r = 0, len(A) - 1\n            ans = 0\n            while l < r:\n                s = A[l] * A[r]\n                if s == target:\n                    i = l + 1\n                    while i < r and A[i] == A[l]:\n                        i += 1\n                    numL = i - l\n                    j = r - 1\n                    while j > l and A[j] == A[r]:\n                        j -= 1\n                    numR = r - j\n                    l = i\n                    r = j\n                    if i > j and A[i] == A[j]:\n                        ans += (r - l) * (r - l - 1) // 2\n                    else:\n                        ans += numL * numR\n                    # print(ans)\n                elif s < target:\n                    l += 1\n                else:\n                    r -= 1\n            return ans\n        \n        ans = 0\n        nums1.sort()\n        nums2.sort()\n        for n in nums1:\n            ans += find(nums2, n * n)\n        for n in nums2:\n            ans += find(nums1, n * n)\n        return ans", "from collections import defaultdict\nclass Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        def solve(aa,bb):\n            res=0\n            cnt=defaultdict(int)\n            for a in aa:\n                for b in bb:\n                    b2=b*b\n                    if b2%a:continue\n                    res+=cnt[b*b//a]\n                cnt[a]+=1\n            return res\n        \n        ans=solve(nums1,nums2)+solve(nums2,nums1)\n        return ans\n", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        from collections import defaultdict\n        if len(nums1) == 1 and len(nums2) == 1:\n            return 0\n        def twoproduct(nsquare,nums):\n            countmap = defaultdict(int)\n            total = 0\n            for num in nums:\n                if nsquare % num == 0:\n                    total += countmap.get(nsquare//num,0)\n                    countmap[num] += 1\n            return total\n        ans = 0\n        for num in nums1:\n            ans += twoproduct(num**2, nums2)\n        for num in nums2:\n            ans += twoproduct(num**2, nums1)\n        return ans\n            \n", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        def getProd(n1, n2):\n            c2 = Counter(n2)\n            res = 0\n            for x in n1:\n                prod = x**2\n                for n in n2:\n                    if prod % n > 0: continue\n                    d = prod // n\n                    if d in c2:\n                        res += c2[d]\n                        #print(str(x) + \\\",\\\" + str(n) + \\\",\\\" + str(d))\n                        if d == n: res -= 1\n            return res\n        r = 0\n        r += getProd(nums1, nums2)\n        r += getProd(nums2, nums1)\n        return r//2\n", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        rev, n1, n2 = 0, Counter(nums1), Counter(nums2)\n        for i in range(len(nums1)-1):\n            for j in range(i+1, len(nums1)):\n                t = (nums1[i] * nums1[j])**(1/2)\n                if t == int(t) and t in n2:rev += n2[t]\n        for i in range(len(nums2)-1):\n            for j in range(i+1,len(nums2)):\n                t = (nums2[i] * nums2[j])**(1/2)\n                if t == int(t) and t in n1:rev += n1[t]\n        return rev", "import bisect\nclass Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        v2i1,v2i2 = dict(), dict()\n        n1, n2 = len(nums1), len(nums2)\n        for i in range(0,n1):\n            if not nums1[i] in v2i1:\n                v2i1[nums1[i]] = []\n            v2i1[nums1[i]].append(i)\n            \n        for i in range(0,n2):\n            if not nums2[i] in v2i2:\n                v2i2[nums2[i]] = []\n            v2i2[nums2[i]].append(i) \n            \n        result = 0\n        \n        for z in nums1:\n            z = z*z\n            for x in v2i2:\n                if (z%x)==0 and (z//x) in v2i2:\n                    y=z//x\n                    if y!=x:\n                        js, ks = v2i2[x], v2i2[y]\n                        for j in js:\n                            k = bisect.bisect(ks,j)\n                            result += len(ks) - k\n                    else:\n                        result += len(v2i2[x])*(len(v2i2[x])-1)//2\n        for z in nums2:\n            z = z*z\n            for x in v2i1:\n                if (z%x)==0 and (z//x) in v2i1:\n                    y=z//x\n                    if y!=x:\n                        js, ks = v2i1[x], v2i1[y]\n                        for j in js:\n                            k = bisect.bisect(ks,j)\n                            result += len(ks) - k\n                    else:\n                        result += len(v2i1[x])*(len(v2i1[x])-1)//2\n        return result", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        cnt1_e = Counter()\n        cnt1_ne = Counter()\n        \n        for i in range(len(nums1)):\n            cnt1_e[nums1[i]**2] += 1\n            for j in range(i+1, len(nums1)):\n                cnt1_ne[nums1[i]*nums1[j]] += 1\n            \n        cnt2_e = Counter()\n        cnt2_ne = Counter()\n        \n        for i in range(len(nums2)):\n            cnt2_e[nums2[i]**2] += 1\n            for j in range(i+1, len(nums2)):\n                cnt2_ne[nums2[i]*nums2[j]] += 1  \n        res = 0\n        for k1, v1 in list(cnt1_e.items()):\n            if k1 in list(cnt2_ne.keys()):\n                res += v1 * cnt2_ne[k1]\n       \n        for k2, v2 in list(cnt2_e.items()):\n            if k2 in list(cnt1_ne.keys()):\n                res += v2 * cnt1_ne[k2]\n         \n        return res\n                    \n            \n            \n", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        triplets = 0\n        n1 = Counter(nums1)\n        n2 = Counter(nums2)\n        \n        for i in range(len(nums1) - 1):\n            for j in range(i+1, len(nums1)):\n                t = (nums1[i] * nums1[j])**(1/2)\n                if t == int(t) and t in n2:\n                    triplets += n2[t]\n                    \n        for i in range(len(nums2) - 1):\n            for j in range(i+1, len(nums2)):\n                t = (nums2[i] * nums2[j])**(1/2)\n                if t == int(t) and t in n1:\n                    triplets += n1[t]\n        \n        return triplets\n        \n        \n", "from collections import defaultdict\n\n\nclass Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        res = 0\n        for num in nums1:\n            res += self.two_product(num ** 2, nums2)\n        for num in nums2:\n            res += self.two_product(num ** 2, nums1)\n        return res\n    \n    def two_product(self, target, nums):\n        dic, res = defaultdict(int), 0\n        for i, num in enumerate(nums):\n            if target % num:\n                continue\n            remain = target // num\n            res += dic[remain]\n            dic[num] += 1\n        return res\n", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        t1 = dict(collections.Counter(nums1))\n        t2 = dict(collections.Counter(nums2))\n        c = 0\n        for j in range(len(nums1)):\n            for k in range(j+1, len(nums1)):\n                temp = math.sqrt(nums1[j] *nums1[k])\n                if temp in t2:\n                    c += t2[temp]\n        for j in range(len(nums2)):\n            for k in range(j+1, len(nums2)):\n                temp = math.sqrt(nums2[j] *nums2[k])\n                if temp in t1:\n                    c += t1[temp]\n        return c", "from collections import Counter\n\nclass Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        rev, n1, n2 = 0, Counter(nums1), Counter(nums2)\n\n        for i in range(len(nums1)-1):\n            for j in range(i+1, len(nums1)):\n                t = (nums1[i] * nums1[j])**(1/2)\n\n                if t == int(t) and t in n2:\n                    rev += n2[t]\n\n        for i in range(len(nums2)-1):\n            for j in range(i+1, len(nums2)):\n                t = (nums2[i] * nums2[j])**(1/2)\n\n                if t == int(t) and t in n1:\n                    rev += n1[t]\n\n        return rev", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        self.ans=0\n        def func(n1, n2):\n            for prod in n1:\n                i, j = 0, len(n2)-1\n                while i<j:\n                    if n2[i]==n2[j]:\n                            if n2[i]*n2[j]!=prod*prod:\n                                break\n                            n=j-i+1\n                            self.ans+=(n*(n-1))//2\n                            #print(self.ans)\n                            break\n                    #print(n2, i, j, prod*prod, n2[i], n2[j], n2[i]*n2[j])\n                    if n2[i]*n2[j]==prod*prod:\n                        temp=n2[i]\n                        t1, t2=0, 0\n                        while i<j and n2[i]==temp:\n                            t1+=1\n                            i+=1\n                        temp=n2[j]\n                        while i<=j and n2[j]==temp:\n                            t2+=1\n                            j-=1\n                        self.ans+=(t1*t2)\n                    elif n2[i]*n2[j]>prod*prod:\n                        j-=1\n                    else:\n                        i+=1\n        nums1.sort()\n        nums2.sort()\n        func(nums1, nums2)\n        #print(self.ans)\n        func(nums2, nums1)\n        return self.ans\n", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        \n        \n        def count(arr,match):\n            n=len(arr)\n            cnt=0\n            for i in range(n):\n                for j in range(i+1,n):\n                    val=math.sqrt(arr[i]*arr[j])\n                   # print(val)\n                    if match.get(val):\n                        cnt+=match[val]\n            return cnt\n                    \n            \n        \n        #square1,square2=collections.defaultdict(int),collections.defaultdict(int)\n        #duplet1,duplet2=collections.defaultdict(int),collections.defaultdict(int)\n        \n        freq1,freq2=collections.Counter(nums1),collections.Counter(nums2)\n        #Num1,Num2=list(freq1.keys()),list(freq2.keys())\n       ## return 1\n        return count(nums1,freq2)+count(nums2,freq1)\n    \n        \n        \n", "import bisect\nclass Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        v2i1,v2i2 = dict(), dict()\n        n1, n2 = len(nums1), len(nums2)\n        for i in range(0,n1):\n            if not nums1[i] in v2i1:\n                v2i1[nums1[i]] = []\n            v2i1[nums1[i]].append(i)\n            \n        for i in range(0,n2):\n            if not nums2[i] in v2i2:\n                v2i2[nums2[i]] = []\n            v2i2[nums2[i]].append(i) \n            \n        result = 0\n        \n        for z in nums1:\n            z = z*z\n            for x in v2i2:\n                if (z%x)==0 and (z//x) in v2i2:\n                    y=z//x\n                    if y!=x:\n                        js, ks = v2i2[x], v2i2[y]\n                        for j in js:\n                            k = bisect.bisect_left(ks,j)\n                            result += len(ks) - k\n                    else:\n                        result += len(v2i2[x])*(len(v2i2[x])-1)//2\n        for z in nums2:\n            z = z*z\n            for x in v2i1:\n                if (z%x)==0 and (z//x) in v2i1:\n                    y=z//x\n                    if y!=x:\n                        js, ks = v2i1[x], v2i1[y]\n                        for j in js:\n                            k = bisect.bisect_left(ks,j)\n                            result += len(ks) - k\n                    else:\n                        result += len(v2i1[x])*(len(v2i1[x])-1)//2\n        return result", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        \n        from collections import Counter\n        \n        C1 = Counter()\n        \n        C2 = Counter()\n        \n        for num in nums1:\n            C1[num * num] += 1\n        \n        for num in nums2:\n            C2[num * num] += 1\n            \n        res = 0\n        \n        for i in range(len(nums1) - 1):\n            for j in range(i + 1, len(nums1)):\n                res += C2[nums1[i] * nums1[j]]\n        \n        for i in range(len(nums2) - 1):\n            for j in range(i + 1, len(nums2)):\n                res += C1[nums2[i] * nums2[j]]\n        \n        return res", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        def helper(nums1: List[int], nums2: List[int]) -> int:\n            res = 0\n            counter = Counter(nums2)\n            for n1 in nums1:\n                sq = n1 * n1\n                for n2 in nums2:\n                    if sq % n2 == 0:\n                        num = sq / n2\n                        res += counter[num]\n                        if n1 == n2:\n                            res -= 1\n            return res\n        \n        return int((helper(nums1, nums2) + helper(nums2, nums1)) / 2)\n", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        result = 0\n        dict1 = dict()\n        dict2 = dict()\n        for x in nums1:\n            if x in dict1:\n                dict1[x] += 1\n            else:\n                dict1[x] = 1\n        for x in nums2:\n            if x in dict2:\n                dict2[x] += 1\n            else:\n                dict2[x]=1\n        for x in nums1:\n            squar = x**2\n            for y in nums2:\n                if (squar/y) in dict2:\n                    if (squar/y) == y:\n                        result += dict2[squar/y] - 1\n                    else:\n                        result += dict2[squar/y]\n        for x in nums2:\n            squar = x**2\n            for y in nums1:\n                if (squar/y) in dict1:\n                    if (squar/y) == y:\n                        result += dict1[squar/y] - 1\n                    else:\n                        result += dict1[squar/y]  \n        return int(result/2)                \n", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        rev, n1, n2 = 0, Counter(nums1), Counter(nums2)\n\n        for i in range(len(nums1)-1):\n            for j in range(i+1, len(nums1)):\n                t = (nums1[i] * nums1[j])**(1/2)\n\n                if t == int(t) and t in n2:\n                    rev += n2[t]\n\n        for i in range(len(nums2)-1):\n            for j in range(i+1, len(nums2)):\n                t = (nums2[i] * nums2[j])**(1/2)\n\n                if t == int(t) and t in n1:\n                    rev += n1[t]\n\n        return rev\n", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        res = 0\n        d1 = collections.defaultdict(list)\n        for i, num in enumerate(nums1):\n            d1[num].append(i)\n        \n        for i in range(len(nums2) - 1):\n            for j in range(i+1, len(nums2)):\n                sqrt = (nums2[i]*nums2[j])**0.5\n                if sqrt == int(sqrt) and sqrt in d1:\n                    res += len(d1[sqrt])\n        d2 = collections.defaultdict(list)\n        for i, num in enumerate(nums2):\n            d2[num].append(i)\n        \n        for i in range(len(nums1) - 1):\n            for j in range(i+1, len(nums1)):\n                sqrt = (nums1[i]*nums1[j])**0.5\n                if sqrt == int(sqrt) and sqrt in d2:\n                    res += len(d2[sqrt])\n        return res", "class Solution:\n    def fact(self, n):   \n        res = 1\n        for i in range(2, n+1): \n            res = res * i \n        return res \n\n    def getCombo(self, n):\n        return int(self.fact(n) / (self.fact(2)  \n                * self.fact(n - 2))) \n    \n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        #similar to the 2 sum problem- keep a dictionary for each list \n        d1 = {}\n        for n in nums1:\n            if n not in d1:\n                d1[n] = 1\n            else:\n                d1[n]+=1\n        d2 = {}\n        for n in nums2:\n            if n not in d2:\n                d2[n] = 1\n            else:\n                d2[n]+=1\n        tot = 0\n        sq1 = 0\n        \n        for n in nums1:\n            s = n*n\n            for d in d2:\n                if s%d == 0:\n                    div = s//d\n                    if div in d2:\n                        if div == d:\n                            sq1+=self.getCombo(d2[div])\n                        else:\n                            tot+=(d2[div]*d2[d])\n\n        tot1 = tot//2\n        tot = 0\n        sq2 = 0\n        for n in nums2:\n            s = n*n\n            for d in d1:\n                if s%d == 0:\n                    div = s//d\n                    if div in d1:\n                        if div == d:\n                            sq2+=self.getCombo(d1[div])\n                        else:\n                            tot+=(d1[div]*d1[d])\n        tot2 = tot//2\n        return tot1+tot2+sq1+sq2\n", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n\n        nums1.sort()\n        nums2.sort()\n\n        def f(n):\n            if n == 2 or n == 1:\n                return 1\n            return f(n - 1) + n - 1\n\n        def yanzheng(a, num):\n            '''\n            \u63a5\u53d7\u4e00\u4e2a\u6570\u5b57a\u548c\u4e00\u4e2a\u6709\u5e8f\u5217\u8868num\n            \u7528\u52a8\u6001\u89c4\u5212\u7b97\u6cd5\u9a8c\u8bc1\u662f\u5426\u5b58\u5728num[i] * num[j] == a*a'''\n\n            a = a * a\n            head = 0\n            end = len(num) - 1\n            count = 0\n            while True:\n                headlen = 1\n                endlen = 1\n                if head >= end:\n                    return count\n                if num[head] * num[end] > a:\n                    end -= 1\n                elif num[head] * num[end] < a:\n                    head += 1\n                elif num[head] * num[end] == a:\n                    if num[head] == num[end]:\n                        count += f(end - head + 1)\n                        head = end\n                    elif num[head] == num[head + 1] or num[end] == num[end - 1]:\n                        while num[head] == num[head + headlen]:\n                            headlen += 1\n                        while num[end] == num[end - endlen]:\n                            endlen += 1\n                        count += headlen * endlen\n                        head += headlen\n                        end -= endlen\n                    else:\n                        count += 1\n                        end -= 1\n\n        count = 0\n        for i in nums1:\n            count += yanzheng(i, nums2)\n        for i in nums2:\n            count += yanzheng(i, nums1)\n\n        return count", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        ans = 0\n        n1 = defaultdict(int)\n        for n in nums1:\n            n1[n] += 1\n        n2 = defaultdict(int)\n        for n in nums2:\n            n2[n] += 1\n        for j in range(len(nums1)):\n            for k in range(j+1, len(nums1)):\n                prod = nums1[j]*nums1[k]\n                ans += n2[prod**0.5]\n        for j in range(len(nums2)):\n            for k in range(j+1, len(nums2)):\n                prod = nums2[j]*nums2[k]\n                ans += n1[prod**0.5]\n        return ans\n", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        n = len(nums1)\n        m = len(nums2)\n        nums1prod = [[nums1[i]*nums1[j] for i in range(n)] for j in range(n)]\n        nums2prod = [[nums2[i]*nums2[j] for i in range(m)] for j in range(m)]\n        nums1sq = [x**2 for x in nums1]\n        nums2sq = [x**2 for x in nums2]\n        \n        hm1sq = Counter(nums1sq)\n        hm2sq = Counter(nums2sq)\n        \n        # print(hm1sq, hm2sq, nums1prod, nums2prod)\n        \n        trip = 0\n        for i in range(n):\n            for j in range(i+1,n):\n                if nums1prod[i][j] in hm2sq:\n                    # print(\\\"1\\\", hm2sq[nums1prod[i][j]], nums1prod[i][j])\n                    trip+=hm2sq[nums1prod[i][j]]\n        \n        for i in range(m):\n            for j in range(i+1,m):\n                if nums2prod[i][j] in hm1sq:\n                    # print(\\\"2\\\",hm1sq[nums2prod[i][j]], nums2prod[i][j])\n                    trip+=hm1sq[nums2prod[i][j]]\n                    \n        return trip\n        \n", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        def getProd(nums):\n            dict1 = {}\n            for i in range(len(nums)):\n                for j in range(len(nums)):\n                    if i != j:\n                        prod = nums[i] * nums[j]\n                        if prod not in dict1:\n                            dict1[prod] = 0\n                        dict1[prod] += 1\n            return dict1\n        res = 0\n        d1 = getProd(nums1)\n        for x in nums2:\n            if x**2 in d1:\n                res += d1[x**2]\n        d2 = getProd(nums2)\n        for x in nums1:\n            if x**2 in d2:\n                res += d2[x**2]\n        return res//2", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        \n        def count(n1, n2):\n            l2 = len(n2)\n            cnt = Counter()\n            for k in range(l2):\n                for j in range(k):\n                    v = n2[k] * n2[j]\n                    cnt[v] += 1\n            ans = 0\n            for x in n1:\n                ans += cnt[x*x]\n            return ans\n        \n        return count(nums1, nums2) + count(nums2, nums1)", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        jk_1 = {}\n        for i in range(len(nums1)):\n            for j in range(len(nums1)):\n                if i!=j:\n                    t = nums1[i]*nums1[j]\n                    if t in jk_1:\n                        jk_1[t]+=1\n                    else: jk_1[t]=1\n        jk_2 = {}\n        for i in range(len(nums2)):\n            for j in range(len(nums2)):\n                if i!=j:\n                    t = nums2[i]*nums2[j]\n                    if t in jk_2:\n                        jk_2[t]+=1\n                    else: jk_2[t]=1\n                        \n                        \n        #print(jk_1, jk_2)\n        c=0\n        for x in nums1:\n            if x*x in jk_2:\n                c+=jk_2[x*x]\n        for x in nums2:\n            if x*x in jk_1:\n                c+=jk_1[x*x]\n        return c//2", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        nums1.sort()\n        nums2.sort()\n        \n        triplets = 0\n        \n        for lst1, lst2 in (nums1, nums2), (nums2, nums1):\n            for square in (n ** 2 for n in lst1):\n                seen = defaultdict(int)\n                for n in lst2:\n                    if square % n == 0:\n                        triplets += seen[square // n]\n                    seen[n] += 1\n                        \n        return triplets\n        \n", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        triplets = 0\n        \n        for lst1, lst2 in (nums1, nums2), (nums2, nums1):\n            for square in (n ** 2 for n in lst1):\n                seen = defaultdict(int)\n                for n in lst2:\n                    if square % n == 0:\n                        triplets += seen[square // n]\n                    seen[n] += 1\n                        \n        return triplets\n        \n", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        c1, c2 = collections.Counter(nums1), collections.Counter(nums2)\n        res = 0\n        for n1 in nums1:\n            x = n1 ** 2\n            for n2 in nums2:\n                if x % n2 == 0 and (x // n2) in c2:\n                    if x // n2 == n2:\n                        res += c2[x // n2] - 1\n                    else:\n                        res += c2[x // n2]\n        for n2 in nums2:\n            x = n2 ** 2\n            for n1 in nums1:\n                if x % n1 == 0 and (x // n1) in c1:\n                    if x // n1 == n1:\n                        res += c1[x // n1] - 1\n                    else:\n                        res += c1[x // n1]\n        return res // 2\n", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        \n        \n        def two(target, nums):\n            counter = collections.defaultdict(int)\n            res = 0\n            for num in nums:\n                if target % num == 0:\n                    res += counter[target // num]\n                counter[num] += 1\n            return res\n                    \n            \n        res = 0\n        for num in nums1:\n            res += two(num * num, nums2)\n        for num in nums2:\n            res += two(num * num, nums1)\n            \n        return res\n            \n        \n        \n#         counter1 = collections.Counter(nums1)\n#         counter2 = collections.Counter(nums2)\n#         nums1 = list(set(nums1))\n#         nums1.sort()\n#         nums2 = list(set(nums2))\n#         nums2.sort()\n        \n#         def helper(nums, target, counter):\n            \n#             res = 0\n#             seen = set()\n#             for num in nums:\n#                 if target % num == 0:\n#                     if target // num == num:\n#                         res += counter[num] * (counter[num]-1) // 2\n#                     elif target // num in seen:\n#                         res += counter[num] * counter[target//num]\n#                 seen.add(num)\n#             return res\n                \n        \n#         def findTriplets(nums1, nums2, counter1, counter2):\n#             res = 0\n#             for num in nums1:\n#                 target = num * num\n#                 res += counter1[num] * helper(nums2, target, counter2)\n#             return res\n        \n#         res = 0\n#         res += findTriplets(nums1, nums2, counter1, counter2)\n#         res += findTriplets(nums2, nums1, counter2, counter1)\n#         return res\n                \n                \n        \n        \n                \n            \n            \n                \n                \n                \n        \n        \n        \n", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        count1 = collections.Counter(nums1)\n        count2 = collections.Counter(nums2)\n        result = 0\n        for num1 in count1:\n            for num2 in count2:\n                if num1**2 % num2 == 0 and num1**2 // num2 in count2:\n                    target = num1**2 // num2\n                    if target == num2:\n                        if count2[num2] > 1:\n                            result += count1[num1]*(count2[num2] * (count2[num2]-1) // 2)\n                    else:\n                        result += count1[num1]*(count2[num2] * count2[target] / 2)\n        for num2 in count2:\n            for num1 in count1:\n                if num2**2 % num1 == 0 and num2**2 // num1 in count1:\n                    target = num2**2 // num1\n                    if target == num1:\n                        if count1[num1] > 1:\n                            result += count2[num2]*(count1[num1] * (count1[num1]-1) // 2)\n                    else:\n                        result += count2[num2]*(count1[num1] * count1[target] / 2)\n        return int(result)", "from collections import defaultdict\nclass Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        \n        nums1_sq = [num**2 for num in nums1]\n        nums2_sq = [num**2 for num in nums2]\n        \n        cache = defaultdict(int)\n        count = 0\n        \n        for index in range(len(nums2)):\n            number = nums2[index]\n            if cache[number]:\n                count += cache[number]\n            for sub_index in range(len(nums1_sq)):\n                square = nums1_sq[sub_index]\n                q, r = divmod(square, number)\n                if not r:\n                    cache[q] += 1\n        \n        cache = defaultdict(int)\n        \n        for index in range(len(nums1)):\n            number = nums1[index]\n            if cache[number]:\n                count += cache[number]\n            for sub_index in range(len(nums2_sq)):\n                square = nums2_sq[sub_index]\n                q, r = divmod(square, number)\n                if not r:\n                    cache[q] += 1\n        \n        return count\n", "\nclass Solution:\n    def find(self, n1, n2, c1):\n        res = 0\n        for j in range(len(n2)):\n            for k in range(j+1, len(n2)):\n                t = n2[j]*n2[k]\n                sqrt = t**0.5\n                if sqrt**2 != t:\n                    continue\n                try:\n                    i = c1[sqrt]\n                except KeyError:\n                    i = 0\n                res += i\n        return res\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        res = 0\n        n1 = set(nums1)\n        n2 = set(nums2)\n        c1 = {}\n        c2 = {}\n        for x in nums1:\n            try:\n                c1[x] += 1\n            except KeyError:\n                c1[x] = 1\n        for x in nums2:\n            try:\n                c2[x] += 1\n            except KeyError:\n                c2[x] = 1\n        res += self.find(nums1, nums2, c1)\n        res += self.find(nums2, nums1, c2)\n        return res", "from collections import Counter\nfrom itertools import combinations\nclass Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        def helper(nums1, nums2):\n            cnt = Counter()\n            for a, b in combinations(nums1, 2):\n                cnt[a * b] += 1\n            return sum(cnt[x * x] for x in nums2)\n        return helper(nums1, nums2) + helper(nums2, nums1)", "class Solution:\n    def calculate(self, nums1, nums2):\n        a = [0] * 100001\n        for i in nums1:\n            a[i] += 1\n        ans = 0\n        for num in nums2:\n            for i in nums1:\n                if num * num % i == 0 and num * num // i <= 100000:\n                    ans += a[num * num // i]\n                    if num == i:\n                        ans -= 1\n        ans = ans // 2\n        return ans\n        \n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        return self.calculate(nums1, nums2) + self.calculate(nums2, nums1)", "from collections import defaultdict\n\nclass Solution:\n    def numTriplets(self, num1: List[int], num2: List[int]) -> int:\n        \n        d11 = defaultdict(list)\n        d12 = defaultdict(list)\n        \n        for i in range(len(num1)):\n            for j in range(i, len(num1)):\n                if i == j:\n                    d11[num1[i]**2].append(i)\n                else:\n                    d12[num1[i]*num1[j]].append((i,j))\n                    \n        d21 = defaultdict(list)\n        d22 = defaultdict(list)\n        \n        for i in range(len(num2)):\n            for j in range(i, len(num2)):\n                if i == j:\n                    d21[num2[i]**2].append(i)\n                else:\n                    d22[num2[i]*num2[j]].append((i,j))\n                    \n        ans = 0\n        \n        for key in d11:\n            if key in d22:\n                ans += len(d11[key])*len(d22[key])\n                \n        for key in d21:\n            if key in d12:\n                ans += len(d21[key])*len(d12[key])\n        \n        return ans", "class Solution:\n    def numTriplets(self, a1: List[int], a2: List[int]) -> int:\n        d={}\n        n1=len(a1)\n        n2=len(a2)\n        for i in range(n1):\n            for j in range(i+1,n1):\n                if a1[i]*a1[j] in list(d.keys()):\n                    d[a1[i]*a1[j]]+=1\n                else:\n                    d[a1[i]*a1[j]]=1\n        c=0\n        for x in a2:\n            if x*x in list(d.keys()):\n                c+=d[x*x]\n        e={}\n        for i in range(n2):\n            for j in range(i+1,n2):\n                if a2[i]*a2[j] in list(e.keys()):\n                    e[a2[i]*a2[j]]+=1 \n                else:\n                    e[a2[i]*a2[j]]=1 \n        \n        for x in a1:\n            if x*x in list(e.keys()):\n                c+=e[x*x]\n        \n        return c\n            \n", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        nums1.sort()\n        nums2.sort()\n        \n        count = 0\n        \n        nums1m = [x * x for x in nums1]\n        nums2m = [x * x for x in nums2]\n        \n        d1 = defaultdict(int)\n        d2 = defaultdict(int)\n        \n        for num in nums1:\n            d1[num] += 1\n        for num in nums2:\n            d2[num] += 1\n        \n        for num in nums1m:\n            for j in range(len(nums2)):\n                if nums2[j] > num:\n                    break\n                if num % nums2[j] != 0:\n                    continue\n                left = num // nums2[j] \n                \n                if left in d2:\n                    if left == nums2[j]:\n                        count += d2[left] - 1\n                    else:\n                        count += d2[left]\n                \n        \n        for num in nums2m:\n            for j in range(len(nums1)):\n                if nums1[j] > num:\n                    break\n                if num % nums1[j] != 0:\n                    continue\n                left = num // nums1[j]\n                if left in d1:\n                    if left == nums1[j]:\n                        count += d1[left] - 1\n                    else:\n                        count += d1[left]\n        \n        return count//2\n", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        def one_type(nums1, nums2):\n            res, counter = 0, collections.Counter(nums2)\n            for num1 in nums1:\n                prod = num1 * num1\n                for num2 in nums2:\n                    target = prod / num2\n                    if target in counter:\n                        res += counter[target] - (target == num1)\n            return res // 2\n\n        return one_type(nums1, nums2) + one_type(nums2, nums1)", "from collections import Counter\nclass Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        res = 0\n        c1 = Counter(nums1)\n        c2 = Counter(nums2)\n        for i in nums1:\n            for j in nums2:\n                j2 = j * j\n                ji = j2 // i\n                if ji * i == j2:\n                    res += c1[j2 // i]\n                    if ji == i:\n                        res -= 1\n                i2 = i * i\n                ij = i2 // j\n                if ij * j == i2:\n                    res += c2[i2 // j]\n                    if ij == j:\n                        res -= 1\n        return res // 2", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        d1, d2 = {}, {}\n        for i in range(len(nums1)):\n            for j in range(i):\n                d1[nums1[i] * nums1[j]] = d1.get(nums1[i] * nums1[j], 0) + 1\n        for i in range(len(nums2)):\n            for j in range(i):\n                d2[nums2[i] * nums2[j]] = d2.get(nums2[i] * nums2[j], 0) + 1\n\n\n        return sum(d1.get(i * i, 0) for i in nums2) + sum(d2.get(j * j, 0) for j in nums1)\n", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        \n        def helper(target,nums):\n            dic=collections.defaultdict(int)\n            count=0\n            for num in nums:\n                if target%num==0 and target//num in dic:\n                    count+=dic[target//num]\n                    \n                dic[num]+=1\n                \n            return count\n        \n        ans=0\n        for x in nums1:\n            \n            ans+=helper(x**2,nums2)\n            \n        for x in nums2:\n            ans+=helper(x**2,nums1)\n            \n        return ans\n            \n                    \n        \n                \n                \n", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        def count(n, nums):\n            cnt, d=0, collections.defaultdict(int)\n            for i in nums:\n                if n%i==0 and n//i in d: \n                    cnt+=d[n//i]\n                d[i]+=1\n            return cnt\n        \n        res=0\n        for i in nums1:\n            res+=count(i*i, nums2)\n        for i in nums2:\n            res+=count(i*i, nums1)\n        return res", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        fin = 0\n        for i in range(len(nums1)):\n            res = 0\n            fin+= self.finder(nums1[i], nums2, res)\n        for i in range(len(nums2)):\n            res = 0\n            fin+= self.finder(nums2[i], nums1, res)\n        return fin\n        \n    def finder(self, square, arr,res):\n        res = 0\n        n = square*square\n        di = {}\n        for i in range(len(arr)):\n            if n%arr[i]==0:\n                p = n//arr[i]\n                if arr[i] in di:\n                    res+=di[arr[i]]\n                if p in di:\n                    di[p] += 1\n                else:\n                    di[p] = 1\n                \n        return res", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        \n        def twoProduct(arr, target):\n            # returns the count of pairs in arr whose product equals target\n            # twoProduct([2,3,1,3,4,6], 6) => 3\n            d = {}\n            ret = 0\n            for x in arr:\n                if target % x == 0 and target // x in d:\n                    ret += d[target//x]\n                if x in d:\n                    d[x] += 1\n                else:\n                    d[x] = 1\n            return ret\n\n        ret = 0\n        for x in nums1:\n            sq = x**2\n            ret += twoProduct(nums2, sq)\n        for x in nums2:\n            sq = x**2\n            ret += twoProduct(nums1, sq)\n        return ret", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        def countTriplets(nums1,nums2):\n            count = 0\n            for index, val in enumerate(nums1):\n                target = int(val**2)\n                lookup = defaultdict(int)\n                for index2, val2 in enumerate(nums2):\n                    if (target / val2) in lookup:\n                        count += lookup[target//val2]\n                    lookup[val2]+=1\n            return count\n        \n        # solving type 1\n        count1 = countTriplets(nums1,nums2)\n        count2 = countTriplets(nums2,nums1)\n        return count1+count2", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        def find(nums1,nums2):\n            res=0\n            for x in nums1:\n                cnt=defaultdict(int)\n                for y in nums2:\n                    if (x*x)%y==0:\n                        res+=cnt[(x*x)//y]\n                    cnt[y]+=1\n            return res\n        ret=0\n        ret+=find(nums1,nums2)\n        ret+=find(nums2,nums1)\n        return ret", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        c1= collections.Counter(nums1)\n        c2= collections.Counter(nums2)\n        r=0\n        for n in nums1:\n            for a in nums2:\n                t = n*n//a\n\n                if t*a == n*n:\n                  \n                    if t== a:\n                        r= r+ (c2[t]-1)/2\n                    else:\n                        r= r+ c2[t]/2\n        for a in nums1:\n            for n in nums2:\n                t= n*n//a\n                if t*a == n*n:\n                    if t== a:\n                        r= r+ (c1[t]-1)/2\n                    else:\n                        r= r+ c1[t]/2\n        return math.floor(r)\n        \n        \n                        \n                        \n                            \n                            \n", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        def twoProduct(target, nums):\n            cnt = 0\n            currMap = collections.defaultdict(lambda: 0)\n\n            for num in nums:\n                # \u5982\u679c target \u6574\u9664 num2\uff0c\u4e14 target // num2 \u5728 map \u4e2d\uff0c\u90a3\u4e48\u7ed3\u679c\u52a0\u4e0a map \u4e2d target // num2 \u7684\u4e2a\u6570\n                if target % num == 0 and target // num in currMap:\n                    cnt += currMap[target // num]\n\n                currMap[num] += 1\n\n            return cnt\n\n        res = 0\n\n        # \u5bf9\u4e8e\u6bcf\u4e00\u4e2a nums1 \u4e2d\u7684\u6570\uff0c\u90fd\u521b\u5efa\u4e00\u4e2a map\n        for num1 in nums1:\n            target = num1 * num1\n            res += twoProduct(target, nums2)\n\n        for num2 in nums2:\n            target = num2 * num2\n            res += twoProduct(target, nums1)\n\n        return res", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        \n        d1={}\n        d2={}\n        for i in nums1:\n            if i in d1:\n                d1[i]+=1\n            else:\n                d1[i]=1\n                \n        for i in nums2:\n            if i in d2:\n                d2[i]+=1\n            else:\n                d2[i]=1\n                \n        count=0\n        for key in d1:\n            for x in d2:\n                if (key**2)%x==0:\n                    if (key**2)//x==x:\n                        if d2[x]>1:\n                            count+=d1[key]*(d2[x]-1)*(d2[x])\n                    else:\n                        if (key**2)//x in d2:\n                            count+=d1[key]*(d2[x])*d2[(key**2)//x]\n                            \n\n        for key in d2:\n            for x in d1:\n                if (key**2)%x==0:\n                    if (key**2)//x==x:\n                        if d1[x]>1:\n                            count+=d2[key]*(d1[x]-1)*(d1[x])\n                    else:\n                        if (key**2)//x in d1:\n                            count+=d2[key]*(d1[x])*d1[(key**2)//x]\n                            \n        return count//2\n                            \n                        \n                    \n                    \n                \n        \n        \n        \n        \n        \n", "from collections import defaultdict\n\nclass Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        return self.triples(nums1, nums2) + self.triples(nums2, nums1)\n        \n        \n    def triples(self, nums1, nums2):\n        res = 0\n        for n1 in nums1:\n            visited = defaultdict(int)\n            for n2 in nums2:\n                if n1 * n1 % n2 == 0:\n                    res += visited[n1 * n1 // n2]\n                \n                visited[n2] += 1\n        \n        return res", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        res = 0\n        for num1 in nums1:\n            currMap = collections.defaultdict(lambda: 0)\n            target = num1 * num1\n\n            for num2 in nums2:\n                if target % num2 == 0 and target // num2 in currMap:\n                    res += currMap[target // num2]\n\n                currMap[num2] += 1\n\n        for num2 in nums2:\n            currMap = collections.defaultdict(lambda: 0)\n            target = num2 * num2\n\n            for num1 in nums1:\n                if target % num1 == 0 and target // num1 in currMap:\n                    res += currMap[target // num1]\n\n                currMap[num1] += 1\n\n        return res", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        def twoProd(B, sq):\n            d = defaultdict(int)\n            cnt = 0\n            for val in B:\n                if sq % val == 0:\n                    cnt += d.get(sq // val, 0)\n                d[val] += 1\n            return cnt\n        \n        def poss(A, B):\n            ret = 0\n            m, n = len(A), len(B)\n            for x in A:\n                ret += twoProd(B, x * x)\n            return ret\n        \n        return poss(nums1, nums2) + poss(nums2, nums1)\n", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        n1, n2 = Counter(nums1), Counter(nums2)\n        \n        rev = 0\n        \n        for i in range(len(nums1)-1):\n            for j in range(i+1, len(nums1)):\n                t = (nums1[i] * nums1[j])**(1/2)\n                \n                if t == int(t) and t in n2:\n                    rev += n2[t]\n        \n        for i in range(len(nums2)-1):\n            for j in range(i+1, len(nums2)):\n                t = (nums2[i] * nums2[j])**(1/2)\n                \n                if t == int(t) and t in n1:\n                    rev += n1[t]\n        \n        return rev", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        comp1, comp2 = defaultdict(set), defaultdict(set)\n        \n        if not nums1 or not nums2: return 0\n        n, m = len(nums1), len(nums2)\n        ans = 0\n        \n        c1, c2 = Counter([num**2 for num in nums1]), Counter([num**2 for num in nums2])\n        \n        for i in range(n):\n            for j in range(i+1, n):\n                prod = nums1[i]*nums1[j]\n                ans += c2[prod]\n                \n        for i in range(m):\n            for j in range(i+1, m):\n                prod = nums2[i]*nums2[j]\n                ans += c1[prod]\n                \n        return ans", "from collections import defaultdict\n\nclass Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        self.res = 0\n        \n        def count(a1, a2):\n            for e1 in [e * e for e in a1]:\n                m = defaultdict(int)\n                for e2 in a2:\n                    if e1 % e2 == 0:\n                        self.res += m[e1 // e2]\n                    m[e2] += 1\n\n        count(nums1, nums2)\n        count(nums2, nums1)\n        \n        return self.res", "from itertools import combinations\nfrom collections import Counter\n\nclass Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        num1Sq = [i*i for i in nums1]\n        num2Sq = [i*i for i in nums2]\n        c1 = Counter(num1Sq)\n        c2 = Counter(num2Sq)\n        \n        comb1 = list(combinations(nums1, 2))    \n        comb2 = list(combinations(nums2, 2))\n        \n        res = 0\n        \n        for i in comb1:\n            res+=c2[i[0]*i[1]]\n        \n        for i in comb2:\n            res+=c1[i[0]*i[1]]\n        \n        return res\n", "def pairs(arr):\n    for i in range(0, len(arr) - 1):\n        for j in range(i + 1, len(arr)):\n            yield (arr[i], arr[j])\n\ndef count(A, B):\n    # Counts the number of triplets of type 1.\n    \n    tally_A = {}\n    for elem in A:\n        tally_A.setdefault(elem ** 2, 0)\n        tally_A[elem ** 2] += 1\n        \n    tally_B = {}\n    for e, f in pairs(B):\n        tally_B.setdefault(e * f, 0)\n        tally_B[e * f] += 1\n        \n    return sum(tally_A[e] * tally_B[e] for e in tally_A if e in tally_B)\n\nclass Solution:\n    def numTriplets(self, A: List[int], B: List[int]) -> int:\n        return count(A, B) + count(B, A)", "from collections import Counter\nfrom itertools import combinations\nclass Solution:\n    def numTriplets(self, nums1, nums2) -> int:\n        c_nums1 = Counter(nums1)\n        c_nums2 = Counter(nums2)\n        res = 0\n        self_product1 = Counter()\n        cross_product1 = Counter()\n        self_product2 = Counter()\n        cross_product2 = Counter()\n\n        for k, v in c_nums1.items():\n            self_product1[k**2] += v*(v-1)//2\n        for k, v in c_nums2.items():\n            self_product2[k**2] += v*(v-1)//2\n\n        for comb in combinations(c_nums1.keys(), 2):\n            p, v = comb\n            cross_product1[p*v] += c_nums1[p]*c_nums1[v]\n        for comb in combinations(c_nums2.keys(), 2):\n            p, v = comb\n            cross_product2[p*v] += c_nums2[p]*c_nums2[v]\n\n        for k,v in c_nums1.items():\n            target = k**2\n            if target in cross_product2:\n                res += v*cross_product2[target]\n            if target in self_product2:\n                res += v*self_product2[target]\n\n        for k,v in c_nums2.items():\n            target = k**2\n            if target in cross_product1:\n                res += v*cross_product1[target]\n            if target in self_product1:\n                res += v*self_product1[target]\n        return res", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        def countTriplets(nums1,nums2):\n            count = 0\n            for index, val in enumerate(nums1):\n                target = int(val**2)\n                lookup = defaultdict(int)\n                for index2, val2 in enumerate(nums2):\n                    if target % val2 == 0 and  (target // val2) in lookup:\n                        count += lookup[target//val2]\n                    lookup[val2]+=1\n            return count\n        \n        # solving type 1\n        count1 = countTriplets(nums1,nums2)\n        count2 = countTriplets(nums2,nums1)\n        return count1+count2", "from collections import Counter\nclass Solution:\n    def mul(self, x):\n        ans = []\n        n = len(x)\n        for i in range(n-1):\n            for j in range(i+1, n):\n                ans.append(x[i] * x[j])\n        return ans\n    def count(self, x, y):\n        ans = 0\n        cx = Counter(x)\n        cy = Counter(y)\n        same = set(x).intersection(set(y))\n        for num in same:\n            # print(num, cx, cy)\n            ans += cx[num] * cy[num]\n        return ans\n            \n    def numTriplets(self, a: List[int], b: List[int]) -> int:\n        ans = 0\n        asq = [i*i for i in a]\n        bsq = [i*i for i in b]\n        aa = self.mul(a)\n        bb = self.mul(b)\n        ans += self.count(asq, bb)\n        ans += self.count(bsq, aa)\n        return ans", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        def count(n1, n2):\n            ret = 0\n            for x in n1:\n                seen = collections.defaultdict(int)\n                for y in n2:\n                    if x*x % y == 0:\n                        ret += seen[x*x // y]\n                    seen[y] += 1    \n            return ret\n        return count(nums1, nums2) + count(nums2, nums1)", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        \n        def cal(arr1, arr2):\n            tmp = 0\n            for n1 in arr1:\n                n1pow2 = n1 * n1\n                h = collections.defaultdict(int)\n                for idx, n2 in enumerate(arr2):\n                    if n1pow2 / n2 in h:\n                        tmp += h[n1pow2 // n2]\n                    h[n2] += 1\n            return tmp\n                    \n        \n        return cal(nums1, nums2) + cal(nums2, nums1)\n                    \n                \n", "from math import sqrt\nclass Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        \n        numTwoHave = defaultdict(lambda: 0)\n        numOneHave = defaultdict(lambda: 0)\n        \n        for j in range(len(nums2)):\n            for k in range(j+1,len(nums2)):\n                numTwoHave[sqrt(nums2[j]*nums2[k])] += 1\n                \n        for j in range(len(nums1)):\n            for k in range(j+1,len(nums1)):\n                numOneHave[sqrt(nums1[j]*nums1[k])] += 1\n        \n        #print(numOneHave)\n        #print(numTwoHave)\n        \n        triplets = 0\n        \n        for num in nums1:\n            if num in numTwoHave:\n                triplets += numTwoHave[num]\n        for num in nums2:\n            if num in numOneHave:\n                triplets += numOneHave[num]\n            \n        return triplets\n", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        res = 0\n        for num in nums1:\n            res += self.two_product(num ** 2, nums2)\n        for num in nums2:\n            res += self.two_product(num ** 2, nums1)\n        return res\n    \n    def two_product(self, target, nums):\n        dic, res = {}, 0\n        for i, num in enumerate(nums):\n            if target % num:\n                continue\n            remain = target // num\n            res += dic.get(remain, 0)\n            dic[num] = dic.get(num, 0) + 1\n        return res\n", "import itertools\ndef calCross(nums):\n    res = []\n    pairs = list(itertools.combinations(nums,2))\n    for (i,j) in pairs:\n        res.append(i*j)\n    return res\n\ndef commonCnt(nums1,nums2):\n    res = 0\n    cross = set(nums1)&set(nums2)\n    for e in cross:\n        res += nums1.count(e)*nums2.count(e)\n    return res                    \n\nclass Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        square1 = [e*e for e in nums1]\n        square2 = [e*e for e in nums2]\n        cross1 = calCross(nums1)\n        cross2 = calCross(nums2)\n        res = commonCnt(square1, cross2)\n        res += commonCnt(square2, cross1)\n        return res", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        def countTriplets(nums1,nums2):\n            count = 0\n            for index, val in enumerate(nums1):\n                target = val**2\n                lookup = defaultdict(int)\n                for index2, val2 in enumerate(nums2):\n                    if target / val2 in lookup:\n                            count += lookup[target//val2]\n                    lookup[val2]+=1\n            return count\n        \n        # solving type 1\n        count1 = countTriplets(nums1,nums2)\n        count2 = countTriplets(nums2,nums1)\n        return count1+count2", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        lookup1 = defaultdict(int)\n\n        def countTriplets(nums1,nums2):\n            count = 0\n            for index, val in enumerate(nums1):\n                target = val**2\n                lookup = defaultdict(int)\n                for index2, val2 in enumerate(nums2):\n                    if target % val2 == 0:\n                        if target // val2 in lookup:\n                            count += lookup[target//val2]\n                    lookup[val2]+=1\n            return count\n        \n        # solving type 1\n        count1 = countTriplets(nums1,nums2)\n        count2 = countTriplets(nums2,nums1)\n        return count1+count2", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        def helper(l1, l2):\n            d = {}\n            for c in l1:\n                if c**2 in d:\n                    d[c**2] += 1\n                else:\n                    d[c**2] = 1\n            \n            r = {}\n            for c in l2:\n                if c in r:\n                    r[c] += 1\n                else:\n                    r[c] = 1\n                    \n            count = 0   \n            for c in l2:\n                for s in d:\n                    res, c2 = s%c, s//c\n                    if res == 0 and c2 in r:\n                        if c2 != c:\n                            count += r[c2] * d[s]\n                        else:\n                            count += (r[c2]-1) * d[s]\n            return count//2\n        \n        return helper(nums1, nums2) + helper(nums2,nums1)", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        ans = 0\n        def cal(n, nums):\n            nonlocal ans\n            d = defaultdict(int)\n            for num in nums:\n                if n % num == 0:\n                    ans += d[n/num]\n                d[num] += 1\n            \n        for n in nums1:\n            cal(n*n, nums2)\n        for n in nums2:\n            cal(n*n, nums1)\n            \n        return ans", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        nums1.sort()\n        nums2.sort()\n        set1 = set(nums1)\n        set2 = set(nums2)\n        lookup1 = defaultdict(int)\n\n        def ncr(n,r):\n            from math import factorial\n            return factorial(n)//factorial(r) //factorial(n-r)\n        \n        def countTriplets(nums1,nums2):\n            lookup = defaultdict(int)\n            count = 0\n            for index, val in enumerate(nums1):\n                target = val**2\n                lookup = defaultdict(int)\n                for index2, val2 in enumerate(nums2):\n                    if target % val2 == 0:\n                        if target // val2 in lookup:\n                            count += lookup[target//val2]\n                    lookup[val2]+=1\n            return count\n        \n        # solving type 1\n        count1 = countTriplets(nums1,nums2)\n        count2 = countTriplets(nums2,nums1)\n        return count1+count2", "#Similar to twoSum problem. \n#Let's consider how we can find if value a ** 2 exists as a product of any \n#   two elements in array [x, y, ..., z]\n#We can store in a set the value of a ** 2 / x for all x in array when fully divisible\n#If the next value in array exists in in set, then we found a match\n#The time complexity for finding one given value is O(N)\nfrom collections import defaultdict\nclass Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        res = 0\n        \n        for num in nums1:\n            res += self.twoProduct(num * num, nums2)\n            \n        for num in nums2:\n            res += self.twoProduct(num * num, nums1)\n        \n        return  res\n    \n    def twoProduct(self, val, nums):\n        prod, res = defaultdict(int), 0\n        for num in nums:\n            res += prod[num]\n            \n            q, r = divmod(val, num)\n            if r == 0: \n                prod[q] += 1\n        \n        return res", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        return self.num(nums1, nums2) + self.num(nums2, nums1)\n        \n    def num(self, nums1, nums2):\n        possibleSquares = {}\n        possibleProducts = {}\n        for (idx, x) in enumerate(nums1):\n            square = x * x \n            if square in possibleSquares:\n                possibleSquares[square].append(idx)\n            else:\n                possibleSquares[square] = [idx]\n        for x in range(len(nums2) - 1):\n            for y in range(x+1, len(nums2)):\n                product = nums2[x] * nums2[y]\n                if product in possibleProducts:\n                    possibleProducts[product].append((x, y))\n                else:\n                    possibleProducts[product] = [(x, y)]\n        result = 0\n        for s in possibleSquares:\n            if s in possibleProducts:\n                result += len(possibleSquares[s]) * len(possibleProducts[s])\n        return result\n", "class Solution:\n    def numTriplets(self, A: List[int], B: List[int]) -> int:\n        res = 0\n        def count(n2, arr):\n            res = 0\n            c = defaultdict(int)\n            for n in arr:\n                if n2 % n == 0:\n                    res += c[n2//n]\n                c[n] += 1\n            return res\n        \n        for i, n in enumerate(A):\n            res += count(n * n, B)\n        for i, n in enumerate(B):\n            res += count(n * n, A)\n        return res\n        \n        \n#         A.sort()\n#         B.sort()\n#         n, m = len(A), len(B)\n#         res = 0\n        \n#         def perm(same):\n#             if same <= 2:\n#                 return 1\n#             return (same - 1) * same // 2\n        \n#         def loop_me(arr, t, m, target):\n#             res = 0\n#             l, r = 0, m - 1\n#             while l < r:\n#                 tmp = arr[l] * arr[r]\n#                 if tmp > t:\n#                     r -= 1\n#                 elif tmp < t:\n#                     l += 1\n#                 else:\n#                     same = 0\n#                     for k in range(l, r + 1):\n#                         if arr[k] == arr[r]:\n#                             same += 1\n#                             # print(k, r)\n#                         else:\n#                             break\n#                     print(\\\"--\\\", target, same, perm(same))\n#                     # print(same)\n#                     res += perm(same) \n#                     break             \n#             return res\n            \n#         for i in range(n):\n#             t = A[i] * A[i]\n#             res += loop_me(B, t, m, A[i])\n\n#         for i in range(m):\n#             t = B[i] * B[i]\n#             res += loop_me(A, t, n, A[i])\n        \n#         return res\n", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        nums1.sort()\n        nums2.sort()\n        count=self.counttrip(nums1,nums2)\n        \n        count+=self.counttrip(nums2,nums1)\n        return int(count)\n        \n    def counttrip(self,nums1,nums2):\n        \n        count =0\n        for i in nums1:\n            \n            \n            target=i*i\n            dictn=defaultdict(int)\n            for j,x in enumerate(nums2):\n                rem = target/x\n                if(rem in dictn):\n                    count+=dictn[rem]\n                dictn[x]+=1\n        return count", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        # nums1 = sorted(nums1)\n        # nums2 = sorted(nums2)\n        res = 0\n        for n1 in nums1:\n            n1 = n1**2\n            visited = collections.defaultdict(int)\n            for n2 in nums2:\n                if n1 % n2 == 0 and visited[n1//n2] > 0:\n                    res += visited[n1//n2]\n                visited[n2] += 1\n                \n        for n2 in nums2:\n            n2 = n2**2\n            visited = collections.defaultdict(int)\n            for n1 in nums1:\n                if n2 % n1 == 0 and visited[n2//n1] > 0:\n                    res += visited[n2//n1]\n                visited[n1] += 1\n        return res", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        nums1.sort()\n        nums2.sort()\n        count=self.counttrip(nums1,nums2)\n        \n        count+=self.counttrip(nums2,nums1)\n        return int(count)\n        \n    def counttrip(self,nums1,nums2):\n        \n        count =0\n        seen=defaultdict(int)\n        for i in nums1:\n            \n            \n            target=i*i\n            # if(target in seen):\n            #     count+=seen[target]\n            #     continue\n            localcount=0\n            dictn=defaultdict(int)\n            for j,x in enumerate(nums2):\n                rem = target/x\n                if(rem in dictn):\n                    localcount+=dictn[rem]\n                dictn[x]+=1\n            seen[target]+=localcount\n            count+=localcount\n        return count", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        if len(nums1) == 1 and len(nums2) == 1:\n            return 0\n        else:\n            ans = 0\n            n1 = Counter(nums1)\n            n2 = Counter(nums2)\n            for i in range(len(nums1)-1):\n                for j in range(i+1, len(nums1)):\n                    t = (nums1[i] * nums1[j])**(1/2)\n\n                    if t == int(t) and t in n2:\n                        ans += n2[t]\n\n            for i in range(len(nums2)-1):\n                for j in range(i+1, len(nums2)):\n                    t = (nums2[i] * nums2[j])**(1/2)\n\n                    if t == int(t) and t in n1:\n                        ans += n1[t]\n            return ans\n                        \n", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        def helper(nums1,nums2):\n            s1=Counter(nums1)\n            s2=Counter(nums2)\n            cnt=0\n            for i in range(len(nums1)):\n                need=nums1[i]*nums1[i]\n                if need==0:\n                    if 0 in s2:\n                        cnt+=s2[0]*(len(num2)-1)-(s2[0]-1)*(s2[0]-1)\n                    else:\n                        cnt+=0\n                else:\n                    for j in range(len(nums2)):\n                        kk=need/nums2[j]\n                        if kk in s2:\n                            if nums2[j]!=kk:\n                                cnt+=s2[kk]/2\n                            else:\n                                cnt+=(s2[kk]-1)/2\n                                \n                                \n                \n            return int(cnt)\n        return helper(nums1,nums2)+helper(nums2,nums1)", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        def helper(nums1, nums2):\n            ret = 0\n            C = collections.Counter(nums2)\n            s = sorted(C)\n            for n in nums1:\n                ret += C[n] * (C[n] - 1) // 2\n                nn = n * n\n                for d in s:\n                    if d >= n:\n                        break\n                    if nn % d == 0:\n                        ret += C[d] * C[nn // d]\n            return ret\n        \n        return helper(nums1, nums2) + helper(nums2, nums1)", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        \n        def tow_product(A, target):\n            \n            d = {}\n            res = 0\n            for x in A:\n                if target%x==0 and target // x in d:\n                    res += d[target//x]\n                d[x] = d.get(x,0) + 1\n            return res\n        \n        def count(nums1, nums2):\n            res = 0\n            last_cnt = 0\n            for i in range(len(nums1)):\n                if i>0 and nums1[i]==nums1[i-1]:\n                    res+=last_cnt\n                else:\n                    last_cnt = tow_product(nums2, nums1[i]**2)\n                    res += last_cnt\n            return res\n        \n        nums1 = sorted(nums1)\n        nums2 = sorted(nums2)\n        return count(nums1, nums2) + count(nums2, nums1)", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        n = len(nums1)\n        m = len(nums2)\n        \n        res = 0\n        for x in nums1 :\n            t = x * x\n            m = {}\n            for y in nums2 :\n                if t % y == 0 :\n                    res += m.get(t//y, 0)\n                m[y] = m.get(y, 0) + 1\n        \n        for y in nums2 :\n            t = y * y\n            m = {}\n            for x in nums1 :\n                if t % x == 0 :\n                    res += m.get(t//x, 0)\n                m[x] = m.get(x, 0) + 1\n        \n        return res\n", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        nums1.sort()\n        nums2.sort()\n        set1 = set(nums1)\n        set2 = set(nums2)\n        lookup1 = defaultdict(int)\n\n        def countTriplets(nums1,nums2):\n            lookup = defaultdict(int)\n            count = 0\n            for index, val in enumerate(nums1):\n                target = val**2\n                lookup = defaultdict(int)\n                for index2, val2 in enumerate(nums2):\n                    if target % val2 == 0:\n                        if target // val2 in lookup:\n                            count += lookup[target//val2]\n                    lookup[val2]+=1\n            return count\n        \n        # solving type 1\n        count1 = countTriplets(nums1,nums2)\n        count2 = countTriplets(nums2,nums1)\n        return count1+count2", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        muti_set1 = set()\n        muti_set2 = set()\n        muti_dict1 = dict()\n        muti_dict2 = dict()\n        \n        for i in range(len(nums1)-1):\n            for j in range(i+1, len(nums1)):\n                v = nums1[i] * nums1[j]\n                if v in muti_set1:\n                    muti_dict1[str(v)] = muti_dict1[str(v)] + 1\n                else:\n                    muti_dict1[str(v)] = 1\n                    muti_set1.add(v)\n            \n        for i in range(len(nums2)-1):\n            for j in range(i+1, len(nums2)):\n                v = nums2[i] * nums2[j]\n                if v in muti_set2:\n                    muti_dict2[str(v)] = muti_dict2[str(v)] + 1\n                else:\n                    muti_dict2[str(v)] = 1\n                    muti_set2.add(v)\n        \n        res = 0\n        print(muti_set1)\n        print(muti_set2)\n        \n        for _ in nums1:\n            if _*_ in muti_set2:\n                res = res + muti_dict2[str(_*_)]\n        \n        for _ in nums2:\n            if _*_ in muti_set1:\n                res = res + muti_dict1[str(_*_)]\n        \n        return res\n", "from collections import defaultdict as dt\nclass Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        \n        cnt = 0\n        for x in nums1:\n            d = dt(int)\n            for y in nums2:\n                if (x * x) % y == 0 and (x*x)//y in d:\n                    cnt += d[(x*x)//y]\n                d[y] += 1\n        \n        for x in nums2:\n            d = dt(int)\n            for y in nums1:\n                if (x * x) % y == 0 and (x*x)//y in d:\n                    cnt += d[(x*x)//y]\n                d[y] += 1\n        return cnt", "# Two sum approach\n\n# returns the number of distinct index pairs (j, k) such that \n# array[j]*array[k] == target\n\nfrom collections import defaultdict\n\n# O(n) time, O(n) space\ndef two_product(array, target):\n    counter = defaultdict(int)\n    count = 0\n    \n    for index, value in enumerate(array):\n        candidate = target / value\n        if candidate in counter:\n            count += counter[candidate]\n        counter[value] += 1\n        # print(f\\\"Counter: {counter}, count: {count}\\\")\n    # print(counter)\n    return count\n    \n\nclass Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        count = 0\n        for num in nums1:\n            count += two_product(nums2, num*num)\n        \n        for num in nums2:\n            count += two_product(nums1, num*num)\n        \n        return count", "from collections import defaultdict\nclass Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        \n        \n        def helper(nums1, nums2):\n            ans = 0\n            for num1 in nums1:\n                exist = defaultdict(int)\n                v = num1 * num1\n                for num2 in nums2:\n                    if v % num2 == 0 and v / num2 in exist:\n                        ans += exist[v / num2]\n                    exist[num2] += 1\n                    \n            return ans\n        return helper(nums1, nums2) + helper(nums2, nums1)\n                \n", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        import collections\n        h1 = {}\n        s1 = collections.Counter()\n        h2 = {}\n        s2 = collections.Counter()\n        for i in range(len(nums1)):\n            for j in range(i + 1, len(nums1)):\n                s1[nums1[i]*nums1[j]] += 1\n                h1[(i, j)] = nums1[i]*nums1[j]\n                \n        for i in range(len(nums2)):\n            for j in range(i + 1, len(nums2)):\n                s2[nums2[i]*nums2[j]] += 1\n                h2[(i, j)] = nums2[i]*nums2[j]\n        ans = 0\n        for i in range(len(nums1)):\n            if nums1[i]*nums1[i] in s2:\n                ans += s2[nums1[i]*nums1[i]]\n                \n        for i in range(len(nums2)):\n            if nums2[i]*nums2[i] in s1:\n                ans += s1[nums2[i]*nums2[i]]\n\n        return ans", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        \n        def cntProduct(square: int, nums: List[int]) -> int:\n            num_count, cnt = {}, 0\n            for num in nums:\n                if square % num == 0:\n                    cnt += num_count.get(square // num, 0)\n                num_count[num] = 1 + num_count.get(num, 0)     \n            return cnt            \n        \n        return sum(cntProduct(num ** 2, nums2) for num in nums1) + sum(cntProduct(num ** 2, nums1) for num in nums2)", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        nums1.sort()\n        nums2.sort()\n        def cnt(tot, arr):\n            mp = {}\n            ans = 0\n            for val in arr:\n                if tot % val == 0 and tot / val in mp:\n                    ans += mp[tot/val]\n                mp[val] = mp.get(val, 0) + 1\n            return ans\n        \n        def helper(arr1, arr2):\n            last_num = None\n            last_ans = None\n            ans = 0\n            for num in arr1:\n                if num == last_num:\n                    ans += last_ans\n                else:\n                    last_num = num\n                    last_ans = cnt(num * num, arr2)\n                    ans += last_ans\n            return ans\n        return helper(nums1, nums2) + helper(nums2, nums1)", "class Solution:\n    def find(self, nums, target):\n        seen = collections.defaultdict(int)\n        res = 0\n        for i in nums:\n            if target % i == 0 and target/i in seen:\n                res += seen[target/i]\n            seen[i] += 1\n        return res\n        \n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        res = 0\n        for i in nums1:\n            target = i * i\n            res += self.find(nums2, target)\n        for i in nums2:\n            target = i * i\n            res += self.find(nums1, target)\n        return res", "# Two sum approach\n\n# returns the number of distinct index pairs (j, k) such that \n# array[j]*array[k] == target\n\nfrom collections import defaultdict\n\n# O(n) time, O(n) space\ndef two_product(array, target):\n    counter = defaultdict(int)\n    count = 0\n    \n    for index, value in enumerate(array):\n        candidate = target / value\n        if candidate in counter:\n            count += counter[candidate]\n        counter[value] += 1\n    return count\n    \n\nclass Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        count = 0\n        for num in nums1:\n            count += two_product(nums2, num*num)\n        \n        for num in nums2:\n            count += two_product(nums1, num*num)\n        \n        return count", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        \n        def helper(nums1, nums2):\n            # n1 = len(num1)\n            # n2 = len(num2)\n            res = 0\n            for num in nums1:\n                val = num ** 2\n                dic = collections.defaultdict(int)\n                for e in nums2:\n                    # if e in dic:\n                    res += dic[e]\n                    dic[val/e] += 1\n            return res\n        return helper(nums1, nums2) + helper(nums2, nums1)\n", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        num12 = [x*x for x in nums1]\n        num22 = [x*x for x in nums2]\n        \n        ans=0\n        #print(num12,num22)\n        \n        for j2 in num22:\n            c2 = Counter() #num2**2 / num1\n            for i in nums1:\n                if j2%i==0:\n                    ans += c2[i]\n                    c2[j2//i]+=1\n        for j1 in num12:\n            c1 = Counter() #num1**2 / num2\n            for i in nums2:\n                if j1%i==0:\n                    ans += c1[i]\n                    c1[j1//i]+=1\n        return ans", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        \n        n1 = len(nums1)\n        n2 = len(nums2)\n        res = 0\n        \n        for i in range(n1):\n            x = nums1[i]**2    \n            mp = defaultdict(int)\n            for j in range(n2):\n                if x%nums2[j]==0:\n                    y = x//nums2[j]\n                    if y in mp:\n                        res += mp[y]\n                mp[nums2[j]]+=1\n                \n        for i in range(n2):\n            x = nums2[i]**2    \n            mp = defaultdict(int)\n            for j in range(n1):\n                if x%nums1[j]==0:\n                    y = x//nums1[j]\n                    if y in mp:\n                        res += mp[y]\n                mp[nums1[j]]+=1\n         \n        return res\n            \n        \n", "from collections import Counter\nfrom math import sqrt\nclass Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        def is_perfect(x):\n            return int(sqrt(x))**2 == x\n        size1, size2 = len(nums1), len(nums2)\n        count1, count2 = Counter(nums1), Counter(nums2)\n        max1, max2 = max(nums1), max(nums2)\n        nums1.sort()\n        nums2.sort()\n        res = 0\n        for i in range(size2):\n            if nums2[i] > max1:\n                break\n            for j in range(i+1, size2):\n                mul = nums2[i]*nums2[j]\n                if is_perfect(mul):\n                    res += count1[int(sqrt(mul))]\n        for i in range(size1):\n            if nums1[i] > max2:\n                break\n            for j in range(i+1, size1):\n                mul = nums1[i]*nums1[j]\n                if is_perfect(mul):\n                    res += count2[int(sqrt(mul))]\n        return res", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        res = 0\n        d1 = collections.defaultdict(int)\n        for i in range(len(nums1)):\n            for j in range(i+1,len(nums1)):\n                sqr = math.sqrt(nums1[i]*nums1[j])\n                if sqr == int(sqr):\n                    d1[int(sqr)] += 1\n        for i in nums2:\n            if i in d1:\n                res += d1[i]\n                \n        d2 = collections.defaultdict(int)\n        for i in range(len(nums2)):\n            for j in range(i+1,len(nums2)):\n                sqr = math.sqrt(nums2[i]*nums2[j])\n                if sqr == int(sqr):\n                    d2[int(sqr)] += 1\n        for i in nums1:\n            if i in d2:\n                res += d2[i]\n        return res\n", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        prod1={}\n        for i in range(len(nums1)-1):\n            for j in range(i+1,len(nums1)):\n                if nums1[i]*nums1[j] not in prod1.keys():\n                    prod1[nums1[i]*nums1[j]] = 1\n                else:\n                    prod1[nums1[i]*nums1[j]] += 1\n        prod2={}\n        for i in range(len(nums2)-1):\n            for j in range(i+1,len(nums2)):\n                if nums2[i]*nums2[j] not in prod2.keys():\n                    prod2[nums2[i]*nums2[j]] = 1\n                else:\n                    prod2[nums2[i]*nums2[j]] += 1\n        \n        cnt = 0\n        for i in nums1:\n            if i*i in prod2.keys():\n                cnt+=prod2[i*i]\n                \n        for i in nums2:\n            if i*i in prod1.keys():\n                cnt+=prod1[i*i]\n    \n        \n\n        return cnt", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        result = 0\n        dic1 = {}\n        dic2 = {}\n        for x in nums1:\n            if x not in dic1:dic1[x] = 0\n            dic1[x] += 1\n        for x in nums2:\n            if x not in dic2:dic2[x] = 0\n            dic2[x] += 1\n        \n        for k,v in dic1.items():\n            for k1,v1 in dic2.items():\n                if k1 < k:\n                    if k**2 % k1 == 0:\n                        if k**2/k1 in dic2:\n                            result += v*v1*dic2[k**2/k1]\n                elif k1 == k:\n                    result += v * v1 * (v1-1) /2\n        \n        for k,v in dic2.items():\n            for k1,v1 in dic1.items():\n                if k1 < k:\n                    if k**2 % k1 == 0:\n                        if k**2/k1 in dic1:\n                            result += v*v1*dic1[k**2/k1]\n                elif k1 == k:\n                    result += v * v1 * (v1-1) /2      \n                \n        return int(result)", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        \n        def count(nums1, nums2):\n            # triplets = set()\n            triplets = 0\n\n            cnt2 = {}\n            for i2, num2 in enumerate(nums2):\n                if num2 not in cnt2:\n                    cnt2[num2] = []\n                cnt2[num2].append(i2)        \n\n            for i1, num1 in enumerate(nums1):\n                # print('stage1', i1, num1)\n                square = num1 ** 2\n\n                for num2j, idxs2j in cnt2.items():\n                    if square % num2j != 0:\n                        continue\n\n                    num2k = int(square // num2j)\n\n                    if num2k not in cnt2:\n                        continue\n\n                    idxs2k = cnt2[num2k]\n                    \n                    combined = []\n                    for idx2j in idxs2j:\n                        combined.append((idx2j, 0))  # j\n                    for idx2k in idxs2k:\n                        combined.append((idx2k, 1))\n                    combined.sort()\n                    temp_cnt = 0\n                    for comb in combined:\n                        idx, ty = comb\n                        if ty == 0:\n                            triplets += temp_cnt\n                        else:\n                            temp_cnt += 1\n                    \n                    \n#                     print(num1, square, idxs2j, idxs2k, j, k)\n#                     while j >= 0:\n#                         print(j, k, larger)\n#                         if idxs2j[j] < idxs2k[k]:\n#                             if k > 0:\n#                                 larger += 1\n#                                 k -= 1\n#                             else:\n#                                 if larger == 0:\n#                                     larger = 1\n#                                 triplets += larger\n#                                 j -= 1\n#                             # if k < 0:\n#                             #     triplets += larger\n#                             #     break\n#                         else:\n#                             triplets += larger\n#                             j -= 1\n#                     # triplets += larger\n                    \n#                     print('triplets=', triplets)\n                    \n                    \n                    # print('stage2', idxs2k)\n                    \n                    # for idx2j in idxs2j:\n                    #     for idx2k in idxs2k:\n                    #         if idx2k <= idx2j:\n                    #             continue\n                    #         triplets += 1\n                            # triplets.add((i1, idx2j, idx2k))\n                            # print(num1, nums2[idx2j], nums2[idx2k])\n\n            return triplets\n            \n        \n        triplets1 = count(nums1, nums2)\n        triplets2 = count(nums2, nums1)\n                \n        # return len(triplets1) + len(triplets2)\n        return triplets1 + triplets2", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        def comb(nums, k):\n            res = 0\n            count = collections.defaultdict(int)\n            for i in range(len(nums)):\n                x = nums[i]\n                y = k * k // x\n                if x * y == k * k:\n                    res += count[y]\n                count[x] += 1\n            return res\n\n        res = 0\n        for x in nums1:\n            res += comb(nums2, x)\n        for x in nums2:\n            res += comb(nums1, x)\n        return res", "class Solution:\n    def formMulitplication(s, a):\n        # a should have been sorted in ascending order \n        n=len(a)\n        square_nums=[]\n        for i in range(n-1):\n            for j in range(i+1, n):\n                p=a[i]*a[j]\n                sqrt_p=int(sqrt(p))\n                if sqrt_p*sqrt_p==p:\n                    square_nums.append(sqrt_p)\n        return square_nums\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        nums1.sort()\n        nums2.sort()\n        \n        countnums1=[0 for i in range(100000+1)]\n        countnums2=[0 for i in range(100000+1)]\n        \n        for i in range(len(nums1)):\n            countnums1[nums1[i]]+=1\n        for i in range(len(nums2)):\n            countnums2[nums2[i]]+=1\n        \n        square_nums1=self.formMulitplication(nums1)\n        result_type1=0\n        for i in range(len(square_nums1)):\n            result_type1+=countnums2[square_nums1[i]]\n        \n        square_nums2=self.formMulitplication(nums2)\n        result_type2=0\n        for i in range(len(square_nums2)):\n            result_type2+=countnums1[square_nums2[i]]\n        \n        return (result_type1+result_type2)\n", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        d1 = defaultdict(list)\n        d2 = defaultdict(list)\n        \n        for i, x in enumerate(nums1):\n            for j, y in enumerate(nums1):\n                if j <= i:\n                    continue\n                r = int(sqrt(x * y))\n                if  r**2 == x * y:\n                    d1[r].append((i , j))\n                    \n        for i, x in enumerate(nums2):\n            for j, y in enumerate(nums2):\n                if j <= i:\n                    continue\n                r = int(sqrt(x * y))\n                if r**2 == x * y:\n                    d2[r].append((i , j))\n        ret = 0        \n        for i in nums1:\n            ret += len(d2[i])\n        for i in nums2:\n            ret += len(d1[i])\n        \n        return ret", "class Solution:\n    # O(n\u00b2)\n    # def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n    #     m , n = len(nums1),len(nums2)\n    #     def fillUpper(m,nums):\n    #         mapp1 = defaultdict(int)\n    #         for i in range(m-1):\n    #             for j in range(i+1,m):\n    #                 mapp1[nums[i]*nums[j]]+=1\n    #         return  mapp1\n    #     mat1 = fillUpper(m,nums1)\n    #     mat2 = fillUpper(n,nums2)\n    #     res = 0\n    #     for i in nums1:\n    #         c=i**2 \n    #         if(c in mat2):\n    #             res+=mat2[c]\n    #     for i in nums2:\n    #         c= i**2 \n    #         if(c in mat1):\n    #             res+=mat1[c]       \n    #     return res\n        \n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        def count(nums1,nums2):\n            res = 0\n            for i in nums1:\n                map1 = defaultdict(int)\n                sq = i**2\n                for j in nums2:\n                    comp = sq/j\n                    if(j in map1):\n                        res+=map1[j]\n                    map1[comp]+=1\n            return res\n        return count(nums1,nums2)+count(nums2,nums1)\n", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        set1 = collections.defaultdict(set)\n        for i, num in enumerate(nums1):\n            set1[num].add(i)\n        set2 = collections.defaultdict(set)\n        for i, num in enumerate(nums2):\n            set2[num].add(i)\n        \n        res = 0\n        for i in range(len(nums2)-1):\n            for j in range(i+1, len(nums2)):\n                if (nums2[i] * nums2[j]) ** 0.5 == int((nums2[i] * nums2[j]) ** 0.5):\n                    m = int((nums2[i] * nums2[j]) ** 0.5)\n                    if m in set1:\n                        # print(m, i, j)\n                        res += len(set1[m])\n        \n        for i in range(len(nums1)-1):\n            for j in range(i+1, len(nums1)):\n                if (nums1[i] * nums1[j]) ** 0.5 == int((nums1[i] * nums1[j]) ** 0.5):\n                    m = int((nums1[i] * nums1[j]) ** 0.5)\n                    if m in set2:\n                        # print(m, i, j)\n                        res += len(set2[m])\n        return res", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        num12 = [x*x for x in nums1]\n        num22 = [x*x for x in nums2]\n        \n        ans=0\n        #print(num12,num22)\n        for j2 in num22:\n            c2 = Counter() #num2**2 / num1\n            for i in nums1:\n                if j2%i==0:\n                    if i in c2:\n                        ans += c2[i]\n                    c2[j2//i]+=1\n        for j1 in num12:\n            c1 = Counter() #num1**2 / num2\n            for i in nums2:\n                if j1%i==0:\n                    if i in c1:\n                        ans += c1[i]\n                    c1[j1//i]+=1\n        return ans", "from collections import defaultdict\n\nclass Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n#         nums1[i] ^ 2 = nums2[j] * nums2[k]\n#          \n\n        def solv(A, B):\n            ans = 0\n            for a in A:\n                x = a**2 \n                seen = defaultdict(lambda: 0)\n                for b in B:\n                    q, r = divmod(x, b)\n                    if r != 0:\n                        seen[b] += 1\n                        continue\n                    ans += seen[q]              \n                    seen[b] += 1\n            return ans\n        \n        return solv(nums1, nums2) + solv(nums2, nums1)\n", "from itertools import combinations\n\nclass Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        \n        def isPerSq(n):\n            return int(math.sqrt(n) + 0.5) ** 2 == n\n        \n        d1 = Counter(nums1)\n        d2 = Counter(nums2)\n        \n        s = 0\n        for x,y in combinations(nums2, 2):\n            if isPerSq(x*y):\n                s += d1[math.sqrt(x*y)]\n        \n        for x,y in combinations(nums1, 2):\n            if isPerSq(x*y):\n                s += d2[math.sqrt(x*y)]\n        \n        return s", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        def generate_nums(nums):\n            nums = sorted(list(collections.Counter(nums).items()))\n            n = len(nums)\n            pow_nums = defaultdict(int)\n            mul_nums = defaultdict(int)\n            for i in range(n):\n                num = nums[i][0]\n                pow_nums[num * num] += nums[i][1]\n                for j in range(i + 1, n):\n                    mul_nums[num * nums[j][0]] += nums[i][1] * nums[j][1]\n\n                mul_nums[num * num] += nums[i][1] * (nums[i][1] - 1) // 2\n                \n            return (pow_nums, mul_nums)\n        \n        (pow_nums1, mul_nums1) = generate_nums(nums1)\n        (pow_nums2, mul_nums2) = generate_nums(nums2)\n        \n        ans = 0\n        \n        def check_triplet(nums, pow_nums):\n            ans = 0\n            for num, count in nums.items():\n                ans += pow_nums[num] * count\n            return ans\n                        \n        ans += check_triplet(mul_nums1, pow_nums2)\n        ans += check_triplet(mul_nums2, pow_nums1)\n        \n        return ans", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        \n        p1={}\n        p2={}\n        c=0\n        for i in range(len(nums1)):\n            for j in range(len(nums1)):\n                if i!=j:\n                    p1[nums1[i]*nums1[j]]=p1.get(nums1[i]*nums1[j],0)+1\n             \n        for i in range(len(nums2)):\n            for j in range(len(nums2)):\n                if i!=j:\n                    p2[nums2[i]*nums2[j]]=p2.get(nums2[i]*nums2[j],0)+1\n             \n        for i in nums1:\n            if i*i in list(p2.keys()):\n                c+=p2[i*i]//2\n        for i in nums2:\n            if i*i in list(p1.keys()):\n                c+=p1[i*i]//2\n        return c\n            \n                \n       \n                \n", "import itertools, math\nfrom collections import Counter\n\nclass Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        \n        def is_perfect_square(n):\n            return int(math.sqrt(n) + 0.5) ** 2 == n\n        \n        nums1_cntr, nums2_cntr = Counter(nums1), Counter(nums2)\n        \n        s = 0\n        for x, y in itertools.combinations(nums2, 2):\n            if is_perfect_square(x * y):\n                s += nums1_cntr[math.sqrt(x * y)]\n                \n        for x, y in itertools.combinations(nums1, 2):\n            if is_perfect_square(x * y):\n                s += nums2_cntr[math.sqrt(x * y)]\n                \n        return s\n    \n                \n", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        ans = 0\n        \n        \n        for n in nums1:\n            n2 = n*n\n            seen = collections.defaultdict(int)\n            for i in range(len(nums2)):\n                ans += seen[n2/nums2[i]]\n                seen[nums2[i]] += 1\n            \n        \n        \n        for n in nums2:\n            n2 = n*n\n            seen = collections.defaultdict(int)\n            for i in range(len(nums1)):\n                ans += seen[n2/nums1[i]]\n                seen[nums1[i]] += 1\n        \n        return ans", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        ans=0 \n        nums1square=[]\n        for i in nums1:\n            nums1square.append(i*i)\n          \n        for i in nums1square:\n            d=dict()\n            for j in nums2:\n                if(i%j==0):\n                    if(i/j in d.keys()):\n                        ans+=d[i/j]\n                    if(j in d.keys()):\n                        d[j]+=1\n                    else:\n                        d[j]=1\n        nums2square=[]\n        for i in nums2:\n            nums2square.append(i*i)\n          \n        for i in nums2square:\n            d=dict()\n            for j in nums1:\n                if(i%j==0):\n                    if(i/j in d.keys()):\n                        ans+=d[i/j]\n                    if(j in d.keys()):\n                        d[j]+=1\n                    else:\n                        d[j]=1\n        \n                \n            \n        return ans", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        \n        res = 0\n        if len(nums1) == 1 and len(nums2) == 1:\n            return 0\n        \n        \n        if set(nums1) == set(nums2) and len(set(nums1)) == 1:\n            res =  len(nums2) * (len(nums2) - 1) // 2 \n            res = res * len(nums1)\n            res = res + (len(nums1) * (len(nums1) - 1) // 2) * len(nums2)\n            return res\n        \n        m1 = self.getmultiply(nums1)\n        m2 = self.getmultiply(nums2)\n        \n        for x in nums1:\n            if x * x in m2:\n                res = res + len(m2[x*x])\n        for y in nums2:\n            if y * y in m1:\n                res = res + len(m1[y*y])\n        \n\n                                \n        return res\n\n        \n    def getmultiply(self,nums):\n        \n        res = {}\n        \n        for x in range(len(nums)):\n            for y in range(x+1,len(nums)):\n                if nums[x] * nums[y] in res and [x,y]:\n                    res[nums[x]*nums[y]].append([x,y])\n                else:\n                    res[nums[x]*nums[y]] = [[x,y]]\n        return res\n \n\n\n\n    def getsq(self,nums):\n        \n        res = {}\n        \n        for x in range(len(nums)):\n            if nums[x] * nums[x] in res :\n                if x not in res[nums[x] * nums[x]]:\n                    res[nums[x] * nums[x]].append(x)\n            else:\n                res[nums[x] * nums[x]] = [x]\n                \n        return res\n", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        # tow product. O(nm), O(n)\n        cnt = 0\n        for num in nums1:\n            target = num * num\n            cnt += self.twoProduct(nums2, target)\n        \n        for num2 in nums2:\n            target2 = num2 * num2\n            cnt += self.twoProduct(nums1, target2)\n        \n        return cnt\n    \n    def twoProduct(self, nums, target):\n        cnt = 0\n        hashmap = collections.defaultdict(int)\n        for i in range(len(nums)):\n            if target / nums[i] in hashmap:\n                cnt += hashmap[target / nums[i]]\n            hashmap[nums[i]] += 1\n            \n        return cnt\n", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        d1={}\n        d2={}\n        for i in nums1:\n            d1[i]=d1.get(i,0)+1\n        for i in nums2:\n            d2[i]=d2.get(i,0)+1\n        ans=0\n        d3={}\n        print(d1,d2)\n        for i in range(len(nums1)):\n            d3={}\n            for j in range(len(nums2)):\n                if (nums1[i]*nums1[i])%nums2[j]==0:\n                    #print(1,nums1[i],nums2[j])\n                    x=(nums1[i]*nums1[i])//nums2[j]\n                    y=nums2[j]\n                    if d3.get((x,y),-1)==-1:\n                        if x==y:\n                            x1=d2.get(x,0)\n                            ans+=(x1*(x1-1))//2\n                            d3[(x,y)]=1\n                        else:    \n                        #print(1,d2,(nums1[i]*nums1[i])//nums2[j])\n                            ans+=d2.get(x,0)*d2.get(y,0) \n                            #d2[nums2[j]]+=1\n                            d3[(x,y)]=1\n                            d3[(y,x)]=1\n        d4={}\n        for i in range(len(nums2)):\n            d4={}\n            for j in range(len(nums1)):\n                if (nums2[i]*nums2[i])%nums1[j]==0:\n                    #d1[nums1[j]]-=1\n                    x=((nums2[i]*nums2[i])//nums1[j])\n                    y=nums1[j]\n                    if d4.get((x,y),-1)==-1:\n                        #print(2,nums2[i],nums1[j])\n                        #print(2,d1,((nums2[i]*nums2[i])//nums1[j]))\n                        if x==y:\n                            x1=d1.get(x,0)\n                            ans+=(x1*(x1-1))//2\n                            d4[(x,y)]=1\n                        else:    \n                            ans+=d1.get(x,0)*d1.get(y,0) \n                            #d1[nums1[j]]+=1\n                            d4[(x,y)]=1\n                            d4[(y,x)]=1\n        return ans", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        def cnt(tot, arr):\n            mp = {}\n            ans = 0\n            for val in arr:\n                if tot % val == 0 and tot / val in mp:\n                    ans += mp[tot/val]\n                mp[val] = mp.get(val, 0) + 1\n            return ans\n        ans = 0\n        for num in nums1:\n            ans += cnt(num * num, nums2)\n        for num in nums2:\n            ans += cnt(num * num, nums1)\n        return ans", "class Solution:\n    def numTriplets(self, nums1, nums2):\n        mp1, mp2 = {}, {}\n        for ele in nums1:\n            if ele in mp1: mp1[ele] += 1\n            else: mp1[ele] = 1\n        for ele in nums2:\n            if ele in mp2: mp2[ele] += 1\n            else: mp2[ele] = 1\n        \n        ans = 0\n        for k1, v1 in mp1.items():\n            tar = k1 ** 2\n            for k2 in sorted(mp2.keys()):\n                if k2 ** 2 > tar: break\n                elif k2 ** 2 == tar:\n                    ans += v1 * mp2[k2] * (mp2[k2] - 1) // 2\n                elif tar % k2 == 0 and (tar // k2) in mp2:\n                    ans += v1 * mp2[k2] * mp2[tar // k2]\n        for k2, v2 in mp2.items():\n            tar = k2 ** 2\n            for k1 in sorted(mp1.keys()):\n                if k1 ** 2 > tar: break\n                elif k1 ** 2 == tar:\n                    ans += v2 * mp1[k1] * (mp1[k1] - 1) // 2\n                elif tar % k1 == 0 and (tar // k1) in mp1:\n                    ans += v2 * mp1[k1] * mp1[tar // k1]\n        return ans", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        ans = 0\n        for num in nums1:\n            ans += self.find_num_pairs(num, nums2)\n        \n        for num in nums2:\n            ans += self.find_num_pairs(num, nums1)\n        return ans\n    \n    def find_num_pairs(self, num, pair_list):\n        pair_dict = {}\n        square_num = num ** 2\n        ans = 0\n        for i, n in enumerate(pair_list):\n            if square_num // n in pair_dict and square_num % n == 0:\n                ans += len(pair_dict[square_num // n])\n                # print(pair_dict[square_num // n], i, square_num // n, n, num)\n            if n not in pair_dict:\n                pair_dict[n] = []\n            pair_dict[n].append(i)\n        \n        return ans\n            \n", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        res = 0\n        s1 = collections.Counter()\n        for n1 in nums1:\n            s2 = collections.Counter()\n            for n2 in nums2:\n                if n1 * n1 % n2 == 0 and n1 * n1 // n2 in s2:\n                    res += s2[n1 * n1 // n2]\n                if n2 * n2 % n1 == 0 and n2 * n2 // n1 in s1:\n                    res += s1[n2 * n2 // n1]\n                s2[n2] += 1\n            s1[n1] += 1\n        return res", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        res = 0\n        for ele in nums1:\n            res += self.twoProduct(ele*ele, nums2)\n\n        for ele in nums2:\n            res += self.twoProduct(ele*ele, nums1)\n        \n        return res\n        \n        \n    def twoProduct(self, prod, nums):\n        d = {}\n        n = len(nums)\n        res = 0\n        for i in range(n):\n            if prod%nums[i]==0 and prod//nums[i] in d:\n                res += d[prod//nums[i]]\n            if nums[i] in d:\n                d[nums[i]] += 1\n            else:\n                d[nums[i]] = 1\n                \n        return res", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        nums1 = sorted(nums1)\n        nums2 = sorted(nums2)\n        count = 0\n        for item in nums1:\n            count += self.get_equal_square(item*item, nums2)\n        for item in nums2:\n            count += self.get_equal_square(item*item, nums1)\n        return count\n    \n    def get_equal_square(self, val, arr):\n        count = 0\n        m = {}\n        for i, item in enumerate(arr):\n            # if val == 16:\n            #     print(i, item, m, arr)\n            if val % item:\n                continue\n            tmp = val / item\n            if m.get(tmp, None):\n                count +=  m.get(tmp)\n            m[item] = m.get(item, 0) + 1\n        return count\n        \n        \n", "from collections import Counter\nimport math\nclass Solution:\n    def f(self,nums1, nums2):\n        n1 = len(nums1)\n        freq_dict_2 = Counter(nums2)\n        answer = 0\n        for pos_a,a in enumerate(nums1):\n            for pos_b in range(pos_a+1,n1):\n                b = nums1[pos_b]\n                c_square = a*b\n                potential_c = math.floor(math.sqrt(c_square))\n                #print(f\\\"{potential_c}**2 = {c_square}\\\")\n                #print(f\\\"{a}{b}{potential_c}\\\")\n                if potential_c**2 == (c_square):\n                    if potential_c in freq_dict_2:\n                        answer += freq_dict_2[potential_c]\n        return answer\n                    \n                    \n            \n        \n        \n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        final_answer = 0\n        final_answer += self.f(nums1,nums2)\n        final_answer += self.f(nums2,nums1)\n        return final_answer\n        \n       \n", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        def c(nums1, nums2):\n            ans = 0\n            for num in nums1:\n                count = defaultdict(int)\n\n                for i in range(len(nums2)):\n                    if nums2[i] in count:\n                        ans += count[nums2[i]]\n\n                    new = num*num\n                    if new % nums2[i] == 0:\n                        count[new//nums2[i]] += 1\n\n            return ans\n\n        ans = c(nums1, nums2) + c(nums2, nums1)\n        return ans", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        ans = 0\n        for i in range(len(nums1)):\n            h1 = {}\n            target = nums1[i] * nums1[i]\n            for j in range(len(nums2)):\n                if target/nums2[j] in h1:\n                    ans += h1[target/nums2[j]]\n                if nums2[j] in h1:\n                    h1[nums2[j]] += 1\n                else:\n                    h1[nums2[j]] = 1\n                \n        for i in range(len(nums2)):\n            h2 = {}\n            target = nums2[i] * nums2[i]\n            for j in range(len(nums1)):\n                if target/nums1[j] in h2:\n                    ans += h2[target/nums1[j]]\n                if nums1[j] in h2:\n                    h2[nums1[j]] += 1\n                else:\n                    h2[nums1[j]] = 1        \n\n        return ans\n    \n", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        count = 0\n        for num1 in nums1:\n            a = num1 * num1\n            hashSet = defaultdict(int)\n            for num2 in nums2:\n                complement = a / num2\n                if complement in hashSet:\n                    count += hashSet[complement]\n                hashSet[num2] += 1\n        for num2 in nums2:\n            a = num2 * num2\n            hashSet = defaultdict(int)\n            for num1 in nums1:\n                complement = a / num1\n                if complement in hashSet:\n                    count += hashSet[complement]\n                hashSet[num1] += 1\n        return count", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        return self.calc(nums1, nums2) + self.calc(nums2, nums1)\n\n    def calc(self, nums1, nums2):\n        d, ans = defaultdict(int), 0\n        for num in nums2:\n            d[num] += 1\n        for i, num1 in enumerate(nums1):\n            visited = {num: False for num in d}\n            for num2 in d:\n                cnt2 = d[num2]\n                if visited[num2]:\n                    continue\n                if num1 * num1 % num2 == 0 and num1 * num1 // num2 in d:\n                    num3, cnt3 = num1 * num1 // num2, d[num1 * num1 // num2]\n                    ans += cnt2 * cnt3 if num2 != num3 else cnt2 * (cnt2 - 1) // 2\n                    visited[num3] = True\n                    visited[num2] = True\n        return ans", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        def twoProduct(list1, list2, target):\n            res = 0\n            counter = collections.defaultdict(int)\n            for j in range(len(list2)):\n                q, r = divmod(target, list2[j])\n                if r == 0 and q in counter:\n                    res += counter[q]\n                counter[list2[j]] += 1\n            return res\n        \n        res = 0\n        for n in nums1:\n            res += twoProduct(nums1, nums2, n ** 2)\n        for n in nums2:\n            res += twoProduct(nums2, nums1, n ** 2)\n        return res", "import sys\ninput = sys.stdin.readline\nfrom collections import *\n\nclass Solution:\n    def calc(self, l1, l2):\n        cnt = Counter(l2)\n        res = 0\n        \n        for i in range(len(l1)):\n            for j in range(len(l2)):\n                if l1[i]*l1[i]%l2[j]==0:\n                    k = l1[i]*l1[i]//l2[j]\n                    res += cnt[k]\n                    \n                    if l2[j]==k:\n                        res -= 1\n        \n        return res//2\n    \n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        return self.calc(nums1, nums2)+self.calc(nums2, nums1)", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        nums1.sort()\n        nums2.sort()\n        ans = 0\n        for i in range(len(nums1)):\n            h1 = {}\n            target = nums1[i] * nums1[i]\n            for j in range(len(nums2)):\n                if target/nums2[j] in h1:\n                    ans += h1[target/nums2[j]]\n                if nums2[j] in h1:\n                    h1[nums2[j]] += 1\n                else:\n                    h1[nums2[j]] = 1\n                \n                \n        for i in range(len(nums2)):\n            h2 = {}\n            target = nums2[i] * nums2[i]\n            for j in range(len(nums1)):\n                if target/nums1[j] in h2:\n                    ans += h2[target/nums1[j]]\n                if nums1[j] in h2:\n                    h2[nums1[j]] += 1\n                else:\n                    h2[nums1[j]] = 1        \n\n        return ans", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:  \n        fin = 0\n        \n        for n in nums1:\n            sq = n * n\n            res = {}\n            ans = defaultdict(list)\n            \n            for i, x in enumerate(nums2):\n                y = sq/x\n                \n                if y in ans:\n                    fin += len(ans[y])\n                ans[x].append(i)\n        \n        \n        for n in nums2:\n            sq = n * n\n            res = set()\n            ans = defaultdict(list)\n            \n            for i, x in enumerate(nums1):\n                y = sq/x\n                \n                if y in ans:\n                    fin += len(ans[y])\n                ans[x].append(i)\n\n        return fin\n", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        res = 0\n        #type1\n        d1 = collections.defaultdict(int)\n        l1 = [i**2 for i in nums1]\n        for i in range(len(nums2)):\n            if nums2[i] in d1:\n                res += d1[nums2[i]]\n            for j in range(len(l1)):\n                if l1[j] % nums2[i] == 0:\n                    d1[l1[j]//nums2[i]] += 1\n        d2 = collections.defaultdict(int)\n        l2 = [i**2 for i in nums2]\n        for i in range(len(nums1)):\n            if nums1[i] in d2:\n                res += d2[nums1[i]]\n            for j in range(len(l2)):\n                if l2[j] % nums1[i] == 0:\n                    d2[l2[j]//nums1[i]] += 1\n        \n        return res", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        ind1, ind2 = defaultdict(list), defaultdict(list)\n        \n        for i, v in enumerate(nums1):\n            ind1[v].append(i)\n        for i, v in enumerate(nums2):\n            ind2[v].append(i)\n        \n        type1, type2 = 0, 0\n        \n        for i, v in enumerate(nums1):\n            for j, v2 in enumerate(nums2):\n                if (v*v)%v2:\n                    continue\n                req = (v*v) // v2\n                # print(i, j, ind2[req])\n                type1 += len(ind2[req]) - (v2 == req)\n        \n        for i, v in enumerate(nums2):\n            for v1 in nums1:\n                if (v*v)%v1:\n                    continue\n                req = (v*v) // v1\n                type2 += len(ind1[req]) - (v1 == req)\n        print(type1, type2)\n        ans = (type1//2) + (type2//2)\n        return ans", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        count = 0\n        for x in nums1:\n            seen = {}\n            v = x*x\n            for i in nums2:\n                if i in seen:\n                    count+=seen[i]\n                seen[v/i] = seen.get(v/i,0)+1\n        for x in nums2:\n            seen = {}\n            v = x*x\n            for i in nums1:\n                if i in seen:\n                    count+=seen[i]\n                seen[v/i] = seen.get(v/i,0)+1\n        return count", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        return self.check_list(nums1, nums2) + self.check_list(nums2, nums1)\n        \n    def check_list(self, nums1, nums2):\n        count = 0\n        for value in nums1:\n            target = value ** 2\n            count += self.find_two_sum(nums2, target)\n            \n        return count\n            \n            \n    def find_two_sum(self, nums, target):\n        found = dict()\n        count = 0\n        for idx, num in enumerate(nums):\n            \n            if target % num != 0:\n                continue\n            \n            search_val = target // num\n            \n            if search_val in found.keys():\n                count += len(found[search_val])\n                \n            if num in found.keys():\n                found[num].append(idx)\n                \n            else:\n                found[num] = [idx]\n                \n        return count", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        def helper(nums1, nums2):\n            target = [i**2 for i in nums1]\n            ans = 0\n            for i in range(len(target)): \n                s = target[i]\n                d = collections.defaultdict(list)\n                for j in range(len(nums2)):\n                    if nums2[j] in d: ans += len(d[nums2[j]])\n                    d[s/nums2[j]].append(j)\n            #print(ans)            \n            return ans\n        return helper(nums1, nums2) + helper(nums2, nums1)\n", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        tmp_dict1 = dict()\n        res = 0\n        for i, v in enumerate(nums1):\n            if v not in tmp_dict1:\n                tmp_dict1[v] = self.get_num_tr(v, nums2)\n            res += tmp_dict1.get(v)\n            \n        tmp_dict2 = dict()\n        for i, v in enumerate(nums2):\n            if v not in tmp_dict2:\n                tmp_dict2[v] = self.get_num_tr(v, nums1)\n            res += tmp_dict2.get(v)\n        return res\n                    \n    def get_num_tr(self, num, nums):\n        n = num * num\n        res = 0\n        dct = dict()\n        for i, v in enumerate(nums):\n            if (n % v == 0):\n                res += dct.get(int(n/v), 0)\n            dct[v] = dct.get(v, 0) + 1\n\n        return res\n            \n", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        return self.triplets(nums1, nums2) + self.triplets(nums2, nums1)\n    \n    def triplets(self, nums1, nums2):\n        nums1_sq = [x*x for x in nums1]\n        n_triplets = 0\n        for i in range(len(nums1_sq)):\n            nums2_lookup = {}\n            for j in range(len(nums2)):\n                if nums1_sq[i] % nums2[j] != 0:\n                    continue\n                num_to_look_for = nums1_sq[i] / nums2[j]\n                count = nums2_lookup.get(num_to_look_for, 0)\n                n_triplets += count\n                if nums2[j] in nums2_lookup:\n                    nums2_lookup[nums2[j]] += 1\n                else:\n                    nums2_lookup[nums2[j]] = 1\n                \n        \n        return n_triplets", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        \n        def get_prod_dic (arr, num_dic):\n           # num_dic = {}\n        \n            i = 0\n            while i < len(arr):\n                j = i + 1\n                while j < len(arr):\n                    p = arr[i] * arr[j]\n                    if p not in num_dic:\n                        num_dic[p] = []\n                    num_dic[p].append([i, j])\n                    j += 1\n                i += 1\n                \n            return\n        \n        n1_dic = {}\n        get_prod_dic(nums1, n1_dic)\n        \n        n2_dic = {}\n        get_prod_dic(nums2, n2_dic)\n        \n        count = 0\n        for n in nums1:\n            p = n * n\n            if p in n2_dic:\n                count += len(n2_dic[p])\n        for n in nums2:\n            p = n * n\n            if p in n1_dic:\n                count += len(n1_dic[p])\n        \n        return count\n            \n        \n        \n        \n", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        res = 0\n        dic1 = {}\n        dic2 = {}\n        for i, e in enumerate(nums1):\n            target = e ** 2\n            for j in range(len(nums2)):\n                if target % nums2[j] == 0:\n                    if nums2[j] in dic1:\n                        res += dic1[nums2[j]]\n                    dic1[target//nums2[j]]=dic1.get(target//nums2[j],0)+1\n            dic1={}\n        \n\n\n        for i, e in enumerate(nums2):\n            target = e ** 2\n            for j in range(len(nums1)):\n                if target % nums1[j] == 0:\n                    if nums1[j] in dic2:\n                        res += dic2[nums1[j]]\n                    dic2[target // nums1[j]] = dic2.get(target // nums1[j], 0) + 1\n            dic2={}\n\n        return res", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        \n        from collections import Counter\n        c1 = Counter(nums1)\n        c2 = Counter(nums2)\n        \n        ans = 0\n        for j in range(len(nums2)):\n            for k in range(j+1, len(nums2)):\n                if math.sqrt(nums2[j]*nums2[k]) in c1:\n                    ans += c1[math.sqrt(nums2[j]*nums2[k])]\n        \n        for j in range(len(nums1)):\n            for k in range(j+1, len(nums1)):\n                if math.sqrt(nums1[j]*nums1[k]) in c2:\n                    ans += c2[math.sqrt(nums1[j]*nums1[k])]\n        \n        return ans\n    \n                \n            \n", "from collections import Counter\nclass Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        c1, c2 = Counter(nums1), Counter(nums2)\n        n, m = len(nums1), len(nums2)\n        ans = 0\n        for i in range (n):\n            for j in range (m):\n                nums2_k = (nums1[i] * nums1[i]) / nums2[j]\n                ans += c2[nums2_k]\n                if nums2_k == nums2[j]:\n                    ans -= 1\n            \n        for i in range (m):\n            for j in range (n):\n                nums1_k = (nums2[i] * nums2[i]) / nums1[j]\n                ans += c1[nums1_k]\n                if nums1_k == nums1[j]:\n                    ans -= 1\n        \n        return ans//2", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        ans = 0\n        D = {}\n        G = {}\n        H = {}\n        N1 = {}\n        N2 = {}\n        \n        for i in range(len(nums1)):\n            N1[nums1[i]] = 1\n            for j in range(i+1, len(nums1)):\n                D[nums1[i] * nums1[j]] = 1\n                \n        for i in range(len(nums2)):\n            N2[nums2[i]] = 1\n            for j in range(i+1, len(nums2)):\n                G[nums2[i] * nums2[j]] = 1\n                \n        for i in range(len(nums1)):\n            if nums1[i]**2 in G:\n                if nums1[i]**2 not in H:\n                    tmp = 0\n                    for x in range(len(nums2)):\n                        if nums1[i]**2 % nums2[x]==0 and (nums1[i]**2)//nums2[x] in N2:\n                            for y in range(x+1, len(nums2)): \n                                if nums2[x] * nums2[y] == nums1[i]**2:\n                                    tmp += 1\n                                \n                    H[nums1[i]**2] = tmp\n                    ans += tmp\n                    \n                else:\n                    ans += H[nums1[i]**2]\n          \n      \n        H = {}\n                    \n        for i in range(len(nums2)):\n            if nums2[i]**2 in D:\n                if nums2[i]**2 not in H:\n                    tmp = 0\n                    for x in range(len(nums1)):\n                        if nums2[i]**2 % nums1[x]==0 and (nums2[i]**2)//nums1[x] in N1:\n                            for y in range(x+1, len(nums1)): \n                                if nums1[x] * nums1[y] == nums2[i]**2:\n                                    tmp += 1\n                                \n                    H[nums2[i]**2] = tmp\n                    ans += tmp\n                    \n                else:\n                    ans += H[nums2[i]**2]\n                    \n        return ans\n            \n                \n            \n", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        # nums1.sort()\n        # nums2.sort()\n        \n        \n        def count(A, B):\n            res = 0\n            n1 = len(A)\n            n2 = len(B)\n            for i in range(n1):\n                target = A[i]**2\n                d = collections.defaultdict(int)\n                for num in B:\n                    if target/num in d:\n                        res += d[target/num]\n                    d[num] += 1\n            return res\n                        \n            \n            \n            \n        return count(nums1, nums2) + count(nums2, nums1)", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        rev, n1, n2 = 0, Counter(nums1), Counter(nums2)\n        \n        for i in range(len(nums1)-1):\n            for j in range(i+1, len(nums1)):\n                t = (nums1[i] * nums1[j])**(1/2)\n                \n                if t == int(t) and t in n2:\n                    rev += n2[t]\n        \n        for i in range(len(nums2)-1):\n            for j in range(i+1, len(nums2)):\n                t = (nums2[i] * nums2[j])**(1/2)\n                \n                if t == int(t) and t in n1:\n                    rev += n1[t]\n        \n        return rev", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        cnt1, cnt2 = collections.Counter(nums1), collections.Counter(nums2)\n        ans = 0\n        for v1, l1 in list(cnt1.items()):\n            for v2, l2 in list(cnt2.items()): \n                # only check half of it, \n                if v2 != v1 and v1 ** 2 / v2 in cnt2: \n                    ans += (l1 * l2 * cnt2[v1**2 / v2])\n                elif v2 == v1: \n                    ans += (l1 * l2 * (l2-1) )\n        for v1, l1 in list(cnt2.items()):\n            for v2, l2 in list(cnt1.items()): \n                # only check half of it, \n                if v2 != v1 and v1 ** 2 / v2 in cnt1: \n                    ans += (l1 * l2 * cnt1[v1**2 / v2] )\n                elif v2 == v1: \n                    ans += (l1 * l2 * (l2-1) )\n        return int(ans / 2)\n            \n            \n", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        \n        res = 0\n        \n        for i in range(len(nums1)):\n            target = nums1[i]**2\n            prev = collections.Counter()\n            for j in range(len(nums2)):\n                if target % nums2[j] != 0: continue\n                val = target // nums2[j]\n                if val in prev:\n                    res += prev[val]\n                prev[nums2[j]] += 1\n        \n        for i in range(len(nums2)):\n            target = nums2[i]**2\n            prev = collections.Counter()\n            for j in range(len(nums1)):\n                if target % nums1[j] != 0: continue\n                val = target // nums1[j]\n                if val in prev:\n                    res += prev[val]\n                prev[nums1[j]] += 1\n        \n        return res\n", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        nums1.sort()\n        nums2.sort()\n        set1 = set(nums1)\n        set2 = set(nums2)\n        lookup1 = defaultdict(int)\n\n        def ncr(n,r):\n            from math import factorial\n            return factorial(n)//factorial(r) //factorial(n-r)\n        \n        def countTriplets(nums1,nums2):\n            lookup = defaultdict(int)\n            count = 0\n            for index, val in enumerate(nums1):\n                target = val**2\n                lookup = defaultdict(int)\n                for index2, val2 in enumerate(nums2):\n                    if target % val2 == 0:\n                        if target // val2 in lookup and target //val2 != val2:\n                            count += lookup[target//val2]\n                        elif target // val2 in lookup and target //val2 == val2:\n                            count += lookup[target//val2]\n                    lookup[val2]+=1\n            return count\n        \n        # solving type 1\n        count1 = countTriplets(nums1,nums2)\n        count2 = countTriplets(nums2,nums1)\n        return count1+count2", "class Solution:\n    def numTriplets(self, a: List[int], b: List[int]) -> int:\n        n=len(a);m=len(b)\n        pro1={}\n        pro2={}\n        for i in range(n):\n            for j in range(n):\n                if i!=j:\n                    pro1[a[i]*a[j]]=pro1[a[i]*a[j]]+1 if a[i]*a[j] in pro1 else 1\n        for i in range(m):\n            for j in range(m):\n                if i!=j:\n                    pro2[b[i]*b[j]]=pro2[b[i]*b[j]]+1 if b[i]*b[j] in pro2 else 1           \n        ans=0\n        for i in range(n):\n            if a[i]**2 in pro2:\n                ans+=pro2[a[i]**2]//2\n        for i in range(m):\n            if b[i]**2 in pro1:\n                ans+=pro1[b[i]**2] //2      \n        return ans        ", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        \n        def helper(l1, l2):\n            ans = 0\n            for e in l1:\n                dp = Counter()\n                tar = e**2\n                for x in l2:\n                    if tar%x == 0:\n                        ans += dp[tar//x]\n                    dp[x] += 1\n            return ans\n        \n        return helper(nums1, nums2) + helper(nums2, nums1)", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        A = collections.defaultdict(int)\n        B = collections.defaultdict(int)\n        \n        ans = 0\n        for i in nums1:\n            a = i*i\n            B.clear()\n            for j in nums2:\n                b = j*j\n                \n                x = a/j\n                if x in B:\n                    # print(x, a, j)\n                    ans += B[x]\n                \n                y = b/i\n                if y in A:\n                    # print(i, b, y , j)\n                    ans += A[y]\n                \n                B[j] += 1\n            \n            A[i] += 1\n        \n        return ans", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        \n        # x = [i for i in range(1000)]\n        # y = [i for i in range(1000)]\n        # ans = 0\n        # for a in x:\n        #     for b in y:\n        #         ans += a*b % 1000000\n        \n        \n        def cal(nums1, nums2):\n            ans = 0\n            for a in nums1:\n                d = collections.defaultdict(lambda:0)\n                sq = a*a\n                for ib in range(0, len(nums2)):\n                    b = nums2[ib]\n                    if b == 0:\n                        if a == 0:\n                            ans += ib\n                        else:\n                            continue\n                    if sq % b != 0:\n                        continue\n                    else:\n                        c = sq/b\n                        if c in d:\n                            ans += d[c]\n                    d[b] += 1\n            print(ans)\n            return ans\n        \n        return cal(nums1,nums2) + cal(nums2,nums1)", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        def helper(nums1,nums2):\n            s = [n*n for n in nums1]\n            count = 0\n            d = collections.defaultdict(list)\n            for i in range(len(nums2)):\n                d[nums2[i]].append(i)\n                \n            for n in s:\n                for i in range(len(nums2)):\n                    if n%nums2[i] == 0 and n//nums2[i] in d:\n                        if nums2[i] == n//nums2[i]:\n                            count += len(d[n//nums2[i]])-1\n                        else:\n                            count += len(d[n//nums2[i]])\n            return count//2\n        return helper(nums1,nums2) + helper(nums2,nums1)", "from collections import Counter\nfrom itertools import combinations \nfrom math import sqrt\nclass Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        \n        count1=Counter(nums1)\n        count2=Counter(nums2)\n        set1=set(nums1)\n        set2=set(nums2)\n        nums11=list([x*x for x in set1])\n        nums22=list([x*x for x in set2])\n        count=0\n        for i,j in list(combinations(set1,2)):\n            if i*j in nums22:\n                count+=count2[sqrt(i*j)]*count1[i]*count1[j]\n        for i,j in list(combinations(set2,2)):\n            if i*j in nums11:\n                count+=count1[sqrt(i*j)]*count2[i]*count2[j]\n        for i in set1:\n            if count1[i]>1 and i*i in nums22:\n                count+=count1[i]*(count1[i]-1)*count2[i]//2\n        for i in set2:\n            if count2[i]>1 and i*i in nums11:\n                count+=count2[i]*(count2[i]-1)*count1[i]//2\n        return count\n\n\n\n        # return count1, count2, set1, set2,nums11,nums22, list(combinations(set1,2))\n", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        def helper(l1,l2):\n            res = 0\n            for i in range(len(l1)):\n                comp = dict()\n                target = l1[i]**2\n                \n                for j in range(len(l2)):\n                        if target/l2[j] in comp:\n                            res+=comp[target/l2[j]]\n                        comp[l2[j]] = comp.get(l2[j],0) + 1\n            return res\n        \n        return helper(nums1,nums2) + helper(nums2,nums1)", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        ans = 0\n        nums1.sort()\n        nums2.sort()\n        for i in range(len(nums2)):\n            ii = nums2[i]**2\n            ar = {}\n            for j in range(len(nums1)):\n                # print(nums1[j], ii, ar)\n                if ii%nums1[j] == 0:\n                    g = ii//nums1[j]\n                else:\n                    ar[nums1[j]] = ar.get(nums1[j],0) + 1\n                    continue\n                \n                # print(nums1[j], g, ii)\n                if ar.get(g) is not None:\n                    ans += ar[g]\n                ar[nums1[j]] = ar.get(nums1[j],0) + 1\n                \n        for i in range(len(nums1)):\n            ii = nums1[i]**2\n            ar = {}\n            for j in range(len(nums2)):\n                # print(nums2[j], ii, ar, nums2)\n                if ii%nums2[j] == 0:\n                    g = ii//nums2[j]\n                else:\n                    ar[nums2[j]] = ar.get(nums2[j],0) + 1\n                    continue\n                \n                # print(nums2[j], g, ii)\n                if ar.get(g) is not None:\n                    ans += ar[g]\n                ar[nums2[j]] = ar.get(nums2[j],0) + 1\n        return ans\n                \n", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        \n        nums1.sort()\n        nums2.sort()\n        \n        def lowerbound(target, left, right, nums):\n            while left < right:\n                mid = left + (right - left) // 2\n\n                if nums[mid] == target:\n                    right = mid\n                elif nums[mid] < target:\n                    left = mid + 1\n                else:\n                    right = mid\n            \n            return left\n        \n        def higherbound(target, left, right, nums):\n            while left < right:\n                mid = left + (right - left) // 2\n                \n                if nums[mid] == target:\n                    left = mid + 1\n                elif nums[mid] < target:\n                    left = mid + 1\n                else:\n                    right = mid\n            \n            return left\n            \n        \n        @lru_cache(maxsize = None)\n        def helper1(n):\n            result = 0\n            \n            for i in range(len(nums1)):\n                if n % nums1[i] != 0:\n                    continue\n                    \n                target = n // nums1[i]\n                \n                # lower bound\n                lower = lowerbound(target, i+1, len(nums1), nums1)\n                higher = higherbound(target, i+1, len(nums1), nums1)\n                \n                result += (higher - lower)\n                \n            return result\n        \n        @lru_cache(maxsize = None)\n        def helper2(n):\n            result = 0\n            \n            for i in range(len(nums2)):\n                if n % nums2[i] != 0:\n                    continue\n                    \n                target = n // nums2[i]\n                \n                # lower bound\n                lower = lowerbound(target, i+1, len(nums2), nums2)\n                higher = higherbound(target, i+1, len(nums2), nums2)\n                \n                result += (higher - lower)\n                \n            return result\n        \n        \n        result = 0\n        for n in nums1:\n            result += helper2(n*n)\n        \n        for n in nums2:\n            result += helper1(n*n)\n        \n        \n        return result\n            \n", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        return self.triplets(nums1,nums2)+self.triplets(nums2,nums1)\n        \n    def triplets(self, nums1: List[int], nums2: List[int]) -> int:\n        count = 0\n        counter = Counter(nums2)\n        for i in range(len(nums1)):\n            target = nums1[i]**2\n            \n            for j in range(len(nums2)):\n                if target/nums2[j] in counter:\n                    if target/nums2[j] == nums2[j]:\n                        count += counter[target/nums2[j]] - 1 \n                    else:\n                        count += counter[target/nums2[j]]\n        return int(count/2)\n", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        dic1 = {}\n        dic2 = {}\n        s1 = []\n        s2 = []\n        r = 0\n        n1 = len(nums1)\n        n2 = len(nums2)\n        for i in range(n1):\n            r += self.helper(nums1[i] * nums1[i], nums2)\n        for i in range(n2):\n            r += self.helper(nums2[i] * nums2[i], nums1)\n        \n        return r\n        \n    def helper(self, prod, arr):\n        d = {}\n        r = 0\n        for i in range(len(arr)):\n            if prod % arr[i] == 0:\n                if prod // arr[i] in d:\n                    r += d[prod // arr[i]]\n            d[arr[i]] = d.get(arr[i], 0) + 1\n        return r", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        \n        def count(nums1, nums2):\n            pairs = []\n            N1, N2 = len(nums1), len(nums2)\n            result = 0\n\n            for i in range(N2):\n                for j in range(i + 1, N2):\n                    pairs.append((nums2[i], nums2[j]))\n            \n            Ns = collections.Counter(nums1)\n            for u, v in pairs:\n                if (u * v) ** 0.5 in Ns:\n                    result += Ns[(u * v) ** 0.5]\n\n            return result\n        \n        return count(nums1, nums2) + count(nums2, nums1)", "from collections import Counter\n\nclass Solution:\n        \n    def search(self, nums1, nums2, counter):\n        for i in range(len(nums1)):\n            if nums1[i] ** 2 > nums2[-1] ** 2:\n                break\n            for j in range(i + 1, len(nums1)):\n                if nums1[i] * nums1[j] > nums2[-1] ** 2:\n                    break\n                target = nums1[i] * nums1[j]\n                self.rst += counter[target]\n\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        if not nums1 or not nums2:\n            return 0\n        nums1.sort()\n        nums2.sort()\n        counter1 = Counter([x**2 for x in nums1])\n        counter2 = Counter([x**2 for x in nums2])\n        self.rst = 0\n        self.search(nums1, nums2, counter2)\n        self.search(nums2, nums1, counter1)\n        return self.rst\n\n                \n                \n                \n", "from math import sqrt\nclass Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        count1 = [0] * 100001\n        count2 = [0] * 100001\n        for i in nums1:\n            count1[i] += 1\n        for i in nums2:\n            count2[i] += 1\n        m = len(nums1)\n        n = len(nums2)\n        ans = 0\n        for i in range(m-1):\n            for j in range(i+1, m):\n                sq = int(sqrt(nums1[i] * nums1[j]))\n                if(sq**2 == nums1[i] * nums1[j]):\n                    ans += count2[sq]\n        for i in range(n-1):\n            for j in range(i+1, n):\n                sq = int(sqrt(nums2[i] * nums2[j]))\n                if(sq**2 == nums2[i] * nums2[j]):\n                    ans += count1[sq]\n        return ans", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        ret1 = self.check(nums1, nums2)\n        ret2 = self.check(nums2, nums1)\n        \n        return (ret1 + ret2)\n\n\n    def check(self, n1 :List[int], n2 :List[int]):\n        count_n2_org = Counter(n2)\n        cnt = 0\n\n        for i in range(len(n1)):\n            square = n1[i]**2\n            count_n2 = count_n2_org.copy()\n\n            for j in range(len(n2)):\n                if (square % n2[j] == 0):\n                    target = square // n2[j]\n\n                    if (count_n2[target] > 0):\n                        count_n2[n2[j]] -= 1\n                        cnt += count_n2[target]\n        \n        \n        return cnt\n", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        return self.helper(nums1, nums2) + self.helper(nums2, nums1)\n        \n    def helper(self, nums1, nums2):\n        res = 0\n        for n in nums1:\n            cnt = {}\n            for m in nums2:\n                q, d = n * n % m, n * n // m \n                if q == 0 and d > 0:\n                    if d in cnt:\n                        res += cnt[d]\n                    cnt[m] = cnt.get(m, 0) + 1\n        return res\n", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        from collections import defaultdict\n        count = 0\n        Dict = defaultdict(int)\n        for i in range(len(nums1)):\n            num = nums1[i]*nums1[i]\n            Dict.clear()\n            for j in range(len(nums2)):\n                if Dict[nums2[j]] > 0:\n                    count += Dict[nums2[j]]\n                if num % nums2[j] == 0:\n                    x = num//nums2[j]\n                    Dict[x] += 1\n        Dict.clear()\n        #print(count)\n        for i in range(len(nums2)):\n            num = nums2[i]*nums2[i]\n            Dict.clear()\n            for j in range(len(nums1)):\n                if Dict[nums1[j]] > 0:\n                    count += Dict[nums1[j]]\n                if num % nums1[j] == 0:\n                    x = num//nums1[j]\n                    Dict[x] += 1\n        return count\n        \n                \n                \n            \n", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        count = 0 \n        \n        dic1 = dict()\n        for num in nums1:\n            if num in dic1:\n                dic1[num] += 1\n            else:\n                dic1[num] = 1\n                \n        dic2 = dict()\n        for num in nums2:\n            if num in dic2:\n                dic2[num] += 1\n            else:\n                dic2[num] = 1\n                     \n        for num1 in nums1:\n            square = num1 ** 2\n            for num2 in nums2:\n                if square / num2 in dic2:\n                    if square / num2 == num2:\n                        count += dic2[square / num2] - 1\n                    else:\n                        count += dic2[square / num2]\n                        \n        for num2 in nums2:\n            square = num2 ** 2\n            for num1 in nums1:\n                if square / num1 in dic1:\n                    if square / num1 == num1:\n                        count += dic1[square / num1] - 1\n                    else:\n                        count += dic1[square / num1]\n                        \n        return int(count / 2)", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        count1 = collections.Counter(nums1)\n        count2 = collections.Counter(nums2)\n        \n        def cal(nums1, count2):\n            ans = 0\n            for n1 in nums1:\n                sq = n1**2\n                for n2 in sorted(count2):\n                    if sq % n2 != 0 or sq // n2 not in count2: \n                        continue\n                    c2 = sq // n2                        \n                    if c2 < n2: \n                        break\n                    elif c2 == n2 and count2[n2] > 1:\n                        ans += math.comb(count2[n2], 2)\n                    elif c2 > n2:\n                        ans += count2[n2] * count2[c2]\n                    \n            return ans\n        \n        return cal(nums1, count2) + cal(nums2, count1)", "class Solution:\n\n    \n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        def twoProduct(n, nums):\n            counter = collections.Counter()\n            res = 0\n            for num in nums:\n                if n % num == 0:\n                    res += counter[n / num]\n                counter[num] += 1\n            return res\n        res = 0\n        for num in nums1:\n            res += twoProduct(num*num, nums2)\n        for num in nums2:\n            res += twoProduct(num*num, nums1)\n        return res\n\n        \n#         res = 0\n#         d1 = collections.defaultdict(list)\n#         for i, num in enumerate(nums1):\n#             d1[num].append(i)\n        \n#         for i in range(len(nums2) - 1):\n#             for j in range(i+1, len(nums2)):\n#                 sqrt = (nums2[i]*nums2[j])**0.5\n#                 if sqrt == int(sqrt) and sqrt in d1:\n#                     res += len(d1[sqrt])\n#         d2 = collections.defaultdict(list)\n#         for i, num in enumerate(nums2):\n#             d2[num].append(i)\n        \n#         for i in range(len(nums1) - 1):\n#             for j in range(i+1, len(nums1)):\n#                 sqrt = (nums1[i]*nums1[j])**0.5\n#                 if sqrt == int(sqrt) and sqrt in d2:\n#                     res += len(d2[sqrt])\n#         return res\n", "from collections import defaultdict\nclass Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        # nums2=sorted(nums2)\n        # def isPresent(p, nums):\n        #     start=0\n        #     end=len(nums)-1\n        #     while(start<end):\n        def get_data(nums1,nums2):\n            count=defaultdict(int)\n            for i in range(len(nums2)):\n                count[nums2[i]]+=1\n            s=0\n            for i in range(len(nums1)):\n                for j in range(len(nums2)):\n                    if (nums1[i]*nums1[i])%nums2[j]!=0:\n                        continue\n                    elif nums2[j]==(nums1[i]*nums1[i])//nums2[j]:   \n                        s+=(count[(nums1[i]*nums1[i])//nums2[j]]-1)\n                    else:\n                        s+=count[(nums1[i]*nums1[i])//nums2[j]]\n            return s//2\n        return get_data(nums1,nums2)+get_data(nums2, nums1)\n", "class Solution:\n    def numTriplets(self, a: List[int], b: List[int]) -> int:\n        \n        # i^2 / k == j\n        \n        def count(a, b):\n            \n            ret = 0\n            \n            for x in a:\n                \n                c = Counter()\n                \n                for y in b:\n                    square = x*x\n                    \n                    if square % y == 0:\n                        ret += c[square // y]\n                    \n                    c[y] += 1\n            \n            return ret\n        \n        return count(a,b) + count(b,a)\n", "class Solution:\n    def numTriplets(self, A: List[int], B: List[int]) -> int:\n        countA, countB = Counter(A), Counter(B)\n        res = 0\n        for i in range(len(A)):\n            x = A[i] * A[i]\n            for j in range(len(B)):\n                if x % B[j] == 0 and x // B[j] in countB:\n                    if B[j] * B[j] == x:\n                        res += (countB[x//B[j]] - 1)\n                        # print(B[j], countB[B[j]] - 1)\n                    else:\n                        res += countB[x//B[j]]\n                        # print(B[j], x, countB[B[j]])\n        # print(res)\n        for i in range(len(B)):\n            x = B[i] * B[i]\n            for j in range(len(A)):\n                if x % A[j] == 0 and x // A[j] in countA:\n                    if A[j] * A[j] == x:\n                        res += (countA[x//A[j]] - 1)\n                    else:\n                        res += countA[x//A[j]]\n        return res//2\n", "class Solution:\n    def find(self, nums, target):\n        seen = collections.defaultdict(int)\n        res = 0\n        for i in nums:\n            if target % i == 0 and target/i in seen:\n                res += seen[target/i]\n            seen[i] += 1\n        return res\n        \n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        nums1.sort()\n        nums2.sort()\n        res = 0\n        for i in nums1:\n            target = i * i\n            res += self.find(nums2, target)\n        for i in nums2:\n            target = i * i\n            res += self.find(nums1, target)\n        return res", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        \n        \n        res = 0\n        for a in nums1:\n            sq = a * a\n            cnt = Counter()\n            for b in nums2:\n                if sq % b == 0 and sq//b in cnt:\n                    res += cnt[sq//b]\n                cnt[b] += 1\n        \n        for a in nums2:\n            sq = a * a\n            cnt = Counter()\n            for b in nums1:\n                if sq % b == 0 and sq//b in cnt:\n                    res += cnt[sq//b]\n                cnt[b] += 1\n        return res\n                    \n        \n        \n        \n        \n        \n        \n", "class Solution:\n    # O(N*M) Solution\n    def numTriplets(self, nums1, nums2): \n        res = 0\n        for n in nums1:\n            res += self.twoProduct(n*n, nums2)\n        for n in nums2:\n            res += self.twoProduct(n*n, nums1)\n        \n        return res\n    \n    \n    def twoProduct(self, target, nums):\n        count = 0\n        compFreq = {} # key: compliment (target/cur), element: freq of this compliment (NOT idx!!!)\n        for i in range(len(nums)):\n            comp = target / nums[i]\n            if comp == int(comp): # is comp is a float, no need to proceed\n                if nums[i] in compFreq:\n                    count += compFreq[nums[i]]\n                \n                if comp in compFreq: # update freq\n                    compFreq[comp] += 1\n                else:\n                    compFreq[comp] = 1\n        return count\n                \n                \n    '''\n    # O(N^2) Solution\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int: \n        res = 0\n        \n        # freq is dict that has key=n^2, element=frequency of n^2 occur\n        # if there are duplicate in nums1=[2,2], and nums2=[1,4], there are 2 cases in stead on 1\n        freq1 = {}\n        freq2 = {}\n        for n in nums1:\n            if n*n in freq1:\n                freq1[n*n] += 1\n            else:\n                freq1[n*n] = 1\n        for n in nums2:\n            if n*n in freq2:\n                freq2[n*n] += 1\n            else:\n                freq2[n*n] = 1\n        \n        for b in range(len(nums2)-1):\n            for c in range(b+1, len(nums2)):\n                if (nums2[b] * nums2[c]) in freq1:\n                    res += freq1[nums2[b] * nums2[c]]\n                    \n        for b in range(len(nums1)-1):\n            for c in range(b+1, len(nums1)):\n                if (nums1[b] * nums1[c]) in freq2:\n                    res += freq2[nums1[b] * nums1[c]]\n        return res\n    '''", "from bisect import bisect_right\nfrom collections import defaultdict\n\ndef sol(inp1, inp2):\n    values1 = defaultdict(list)\n    values2 = defaultdict(list)\n    inp1.sort()\n    inp2.sort()\n    for i1, v1 in enumerate(inp1):\n        values1[v1].append(i1)\n    for i2, v2 in enumerate(inp2):\n        values2[v2].append(i2)\n    count = 0\n    for i1, v1 in enumerate(inp1):\n        for i2, v2 in enumerate(inp2):\n            if v2 > v1:\n                break\n            matches = values2[(v1*v1)/v2]\n            ins = bisect_right(matches, i2)\n            count += len(matches) - ins\n    for i2, v2 in enumerate(inp2):\n        for i1, v1 in enumerate(inp1):\n            if v1 > v2:\n                break\n            matches = values1[(v2*v2)/v1]\n            ins = bisect_right(matches, i1)\n            count += len(matches) - ins\n    return count\n\nclass Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        return sol(nums1, nums2)", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        from collections import defaultdict\n        import math\n        cnt1=defaultdict(int)\n        cnt2=defaultdict(int)\n        for k in range(len(nums1)-1):\n            for j in range(k+1,len(nums1)):\n                temp=nums1[k]*nums1[j]\n                if temp-(math.ceil((temp)**0.5))**2==0:\n                    cnt1[temp]+=1\n        \n        for k in range(len(nums2)-1):\n            for j in range(k+1,len(nums2)):\n                temp=nums2[k]*nums2[j]\n                if temp-(math.ceil((temp)**0.5))**2==0:\n                    cnt2[temp]+=1\n        print(cnt1,cnt2)      \n        res=0            \n        for t in nums1:\n            res+=cnt2[t**2]\n        for t in nums2:\n            res+=cnt1[t**2]\n            \n        return res\n        \n            \n            \n        \n                    \n        \n            \n        return res", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        num1_dict = collections.defaultdict(int)\n        num2_dict = collections.defaultdict(int)\n        for num in nums1:\n            num1_dict[num] += 1\n        for num in nums2:\n            num2_dict[num] += 1\n        \n        res = 0\n        for num in num1_dict:\n            double = num**2\n            for num2 in num2_dict:\n                if double%num2 == 0 and double//num2 in num2_dict:\n                    if num2 == double//num2:\n                        res += num2_dict[num2] * (num2_dict[num2]-1)*num1_dict[num]\n                        \n                    else:\n                        res += num2_dict[num2] * (num2_dict[double//num2])*num1_dict[num]\n                        \n                        \n\n                        \n        for num in num2_dict:\n            double = num**2\n            for num2 in num1_dict:\n                if double%num2 == 0 and double//num2 in num1_dict:\n                    if num2 == double//num2:\n                        res += num1_dict[num2] * (num1_dict[num2]-1)*num2_dict[num]\n                    else:\n                        res += num1_dict[num2] * (num1_dict[double//num2])  *num2_dict[num]\n        return res//2\n", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        \n        nums1.sort()\n        nums2.sort()\n        \n        def lowerbound(target, left, right, nums):\n            while left < right:\n                mid = left + (right - left) // 2\n\n                if nums[mid] == target:\n                    right = mid\n                elif nums[mid] < target:\n                    left = mid + 1\n                else:\n                    right = mid\n            \n            return left\n        \n        def higherbound(target, left, right, nums):\n            while left < right:\n                mid = left + (right - left) // 2\n                \n                if nums[mid] == target:\n                    left = mid + 1\n                elif nums[mid] < target:\n                    left = mid + 1\n                else:\n                    right = mid\n            \n            return left\n            \n    \n        def helper(n, nums, memo):\n            if n in memo:\n                return memo[n]\n            \n            result = 0\n            \n            for i in range(len(nums)):\n                if n % nums[i] != 0:\n                    continue\n                    \n                target = n // nums[i]\n                \n                lower = lowerbound(target, i+1, len(nums), nums)\n                higher = higherbound(target, i+1, len(nums), nums)\n                \n                result += (higher - lower)\n            \n            memo[n] = result\n            return result\n        \n        result = 0\n        \n        memo1 = {}\n        for n in nums1:\n            result += helper(n*n, nums2, memo1)\n        \n        memo2 = {}\n        for n in nums2:\n            result += helper(n*n, nums1, memo2)\n        \n        \n        return result\n            \n", "class Solution:\n    # O(N*M) Solution\n    def numTriplets(self, nums1, nums2): \n        res = 0\n        for n in nums1:\n            res += self.twoProduct(n*n, nums2)\n        for n in nums2:\n            res += self.twoProduct(n*n, nums1)\n        \n        return res\n    \n    \n    def twoProduct(self, target, nums):\n        count = 0\n        compFreq = {} # key: compliment (target/cur), element: freq of this compliment (NOT idx!!!)\n        cList = set()\n        for i in range(len(nums)):\n            comp = target / nums[i]\n            if comp == int(comp): # is comp is a float, no need to proceed\n                if nums[i] in compFreq:\n                    count += compFreq[nums[i]]\n                \n                    \n                \n                if comp in compFreq: # update freq\n                    compFreq[comp] += 1\n                else:\n                    compFreq[comp] = 1\n        return count\n                \n                \n    '''\n    # O(N^2) Solution\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int: \n        res = 0\n        \n        # freq is dict that has key=n^2, element=frequency of n^2 occur\n        # if there are duplicate in nums1=[2,2], and nums2=[1,4], there are 2 cases in stead on 1\n        freq1 = {}\n        freq2 = {}\n        for n in nums1:\n            if n*n in freq1:\n                freq1[n*n] += 1\n            else:\n                freq1[n*n] = 1\n        for n in nums2:\n            if n*n in freq2:\n                freq2[n*n] += 1\n            else:\n                freq2[n*n] = 1\n        \n        for b in range(len(nums2)-1):\n            for c in range(b+1, len(nums2)):\n                if (nums2[b] * nums2[c]) in freq1:\n                    res += freq1[nums2[b] * nums2[c]]\n                    \n        for b in range(len(nums1)-1):\n            for c in range(b+1, len(nums1)):\n                if (nums1[b] * nums1[c]) in freq2:\n                    res += freq2[nums1[b] * nums1[c]]\n        return res\n    '''", "from collections import Counter\nclass Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        da=Counter(nums1)\n        db=Counter(nums2)\n        cnt=0\n        #type1\n        for i in range(len(nums2)):\n            for j in range(i+1,len(nums2)):\n                p=nums2[i]*nums2[j]\n                if math.isqrt(p) ** 2 == p and math.isqrt(p) in da:\n                    cnt+=da[int(math.sqrt(p))]\n        #type2\n        for i in range(len(nums1)):\n            for j in range(i+1,len(nums1)):\n                p=nums1[i]*nums1[j]\n                if math.isqrt(p) ** 2 == p and math.isqrt(p) in db:\n                    cnt+=db[int(math.sqrt(p))]\n        return cnt\n                    \n\n        \n", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        memo1, memo2 = collections.Counter(), collections.Counter()\n        for a, b in itertools.combinations(nums1, 2):\n            temp = int(math.sqrt(a * b))\n            if temp ** 2 == a * b:\n                memo1[temp] += 1\n        for a, b in itertools.combinations(nums2, 2):\n            temp = int(math.sqrt(a * b))\n            if temp ** 2 == a * b:\n                memo2[temp] += 1\n        return sum(memo2[num] for num in nums1) + sum(memo1[num] for num in nums2)", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        r = 0\n        c = Counter()\n        for i in nums1:\n            if i in c:\n                r += c[i]\n            for j in nums2:\n                c[j*j/i] += 1\n                \n        c = Counter()\n        for i in nums2:\n            if i in c:\n                r += c[i]\n            for j in nums1:\n                c[j*j/i] += 1\n        return r", "from collections import defaultdict\nclass Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        sq1 = defaultdict(list)\n        pd1 = defaultdict(list)\n        sq2 = defaultdict(list)\n        pd2 = defaultdict(list)\n        \n        for i in range(0,len(nums1)):\n            n1 = nums1[i]\n            sq1[n1*n1].append(i)\n            for j in range(i+1, len(nums1)):\n                n2 = nums1[j]\n                pd1[n1*n2].append([i,j])\n                \n        for i in range(0,len(nums2)):\n            n1 = nums2[i]\n            sq2[n1*n1].append(i)\n            for j in range(i+1, len(nums2)):\n                n2 = nums2[j]\n                pd2[n1*n2].append([i,j])\n        \n        type1 = 0\n        type2 = 0\n        \n        for sq in sq1:\n            if sq in pd2:\n                type1 += (len(sq1[sq])*len(pd2[sq]))\n        for sq in sq2:\n            if sq in pd1:\n                type2 += (len(sq2[sq])*len(pd1[sq]))   \n        return type1 + type2", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        ans = 0\n        n = len(nums1)\n        m = len(nums2)\n        sq1 = list(map(lambda x:x**2,nums1))\n        sq2 = list(map(lambda x:x**2,nums2))\n        d1 = dict()\n        d2 = dict()\n        for num1 in list(set(nums1)):\n            d1[num1] = dict()\n            for i in range(n):\n                if nums1[i] == num1:\n                    if i == 0:\n                        d1[num1][i] = 1\n                    else:\n                        d1[num1][i] = d1[num1][i-1]+1\n                else:\n                    if i == 0:\n                        d1[num1][i] = 0\n                    else:\n                        d1[num1][i] = d1[num1][i-1]\n        \n        for num2 in list(set(nums2)):\n            d2[num2] = dict()\n            for i in range(m):\n                if nums2[i] == num2:\n                    if i == 0:\n                        d2[num2][i] = 1\n                    else:\n                        d2[num2][i] = d2[num2][i-1]+1\n                else:\n                    if i == 0:\n                        d2[num2][i] = 0\n                    else:\n                        d2[num2][i] = d2[num2][i-1]\n        \n        for i in range(n):\n            for j in range(m-1):\n                tmp = sq1[i]//nums2[j]\n                if sq1[i]%nums2[j] == 0:\n                    if tmp not in d2:\n                        continue\n                    ans += d2[tmp][m-1] - d2[tmp][j]\n        \n        for j in range(m):\n            for i in range(n-1):\n                tmp = sq2[j]//nums1[i]\n                if sq2[j]%nums1[i] == 0:\n                    if tmp not in d1:\n                        continue\n                    ans += d1[tmp][n-1] - d1[tmp][i]\n        return ans", "from collections import defaultdict\nclass Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        count = 0\n\n        for n in nums1:\n            t = defaultdict(int)\n            for m in nums2:\n                if (n*n) % m == 0 and ((n*n) // m) in t:\n                    count += t[(n*n) // m]\n                t[m] += 1\n        \n        for n in nums2:\n            t = defaultdict(int)\n            for m in nums1:\n                if (n*n) % m == 0 and ((n*n) // m) in t:\n                    count += t[(n*n) // m]\n                t[m] += 1    \n\n        return count\n                \n", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        return self.helper(nums1, nums2) + self.helper(nums2, nums1)\n    \n    def helper(self, arr1, arr2):\n        res = 0\n        for num in arr1:\n            res += self.twoProduct(num * num, arr2)\n        return res\n    \n    def twoProduct(self, target, arr):\n        visited = {}\n        res = 0\n        for num in arr:\n            if not num:\n                continue\n            if target % num == 0 and target // num in visited:\n                res += visited[target // num]\n            if num not in visited:\n                visited[num] = 1\n            else:\n                visited[num] += 1\n        return res", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n               \n        res = 0\n        cnt1 = collections.Counter()\n        for j in range(len(nums2)):\n            for i in range(len(nums1)):\n                s = nums1[i]**2\n                if s%nums2[j] == 0 and s//nums2[j] in cnt1:\n                    res += cnt1[s//nums2[j]]    \n                \n            cnt1[nums2[j]] +=1\n                    \n        cnt2 = collections.Counter()\n        for j in range(len(nums1)):\n            for i in range(len(nums2)):\n                s = nums2[i]**2\n                if s%nums1[j] == 0 and s//nums1[j]  in cnt2:\n                    res += cnt2[ s//nums1[j] ]    \n                \n            cnt2[nums1[j]] +=1 \n                    \n        return res\n        \n                    \n        \n", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        nums1.sort()\n        nums2.sort()\n        count=self.counttrip(nums1,nums2)\n        \n        count+=self.counttrip(nums2,nums1)\n        return int(count)\n        \n    def counttrip(self,nums1,nums2):\n        \n        count =0\n        seen=defaultdict(int)\n        for i in nums1:\n            \n            \n            target=i*i\n            if(target in seen):\n                count+=seen[target]\n                continue\n            localcount=0\n            dictn=defaultdict(int)\n            for j,x in enumerate(nums2):\n                rem = target/x\n                if(rem in dictn):\n                    localcount+=dictn[rem]\n                dictn[x]+=1\n            seen[target]+=localcount\n            count+=localcount\n        return count", "class Solution:\n    def numTriplets(self, nums1, nums2): \n        res = 0\n        for n in nums1:\n            res += self.twoProduct(n*n, nums2)\n        for n in nums2:\n            res += self.twoProduct(n*n, nums1)\n        \n        return res\n    \n    \n    def twoProduct(self, target, nums):\n        count = 0\n        compFreq = {}\n        for i in range(len(nums)):\n            comp = target / nums[i]\n            if comp == int(comp): # is comp is a float, no need to proceed\n                if nums[i] in compFreq:\n                    count += compFreq[nums[i]]\n                \n                if comp in compFreq:\n                    compFreq[comp] += 1\n                else:\n                    compFreq[comp] = 1\n        return count\n                \n                \n    '''\n    # O(N^2) Solution\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int: \n        res = 0\n        \n        # freq is dict that has key=n^2, element=frequency of n^2 occur\n        # if there are duplicate in nums1=[2,2], and nums2=[1,4], there are 2 cases in stead on 1\n        freq1 = {}\n        freq2 = {}\n        for n in nums1:\n            if n*n in freq1:\n                freq1[n*n] += 1\n            else:\n                freq1[n*n] = 1\n        for n in nums2:\n            if n*n in freq2:\n                freq2[n*n] += 1\n            else:\n                freq2[n*n] = 1\n        \n        for b in range(len(nums2)-1):\n            for c in range(b+1, len(nums2)):\n                if (nums2[b] * nums2[c]) in freq1:\n                    res += freq1[nums2[b] * nums2[c]]\n                    \n        for b in range(len(nums1)-1):\n            for c in range(b+1, len(nums1)):\n                if (nums1[b] * nums1[c]) in freq2:\n                    res += freq2[nums1[b] * nums1[c]]\n        return res\n    '''", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        def helper(alist,blist):\n            res = 0\n            for a in alist:\n                bset = collections.Counter()\n                a2 = a*a\n                for b in blist:\n                    if a2/b in bset: res+=bset[a2/b]\n                    bset[b]+=1\n            return res\n        return helper(nums1,nums2)+helper(nums2,nums1)\n", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        ans = 0\n        for i in range(len(nums1)):\n            h1 = {}\n            target = nums1[i] * nums1[i]\n            for j in range(len(nums2)):\n                if target/nums2[j] in h1:\n                    ans += h1[target/nums2[j]]\n                if nums2[j] in h1:\n                    h1[nums2[j]] += 1\n                else:\n                    h1[nums2[j]] = 1\n                \n                \n        for i in range(len(nums2)):\n            h2 = {}\n            target = nums2[i] * nums2[i]\n            for j in range(len(nums1)):\n                if target/nums1[j] in h2:\n                    ans += h2[target/nums1[j]]\n                if nums1[j] in h2:\n                    h2[nums1[j]] += 1\n                else:\n                    h2[nums1[j]] = 1        \n\n        return ans\n    \n", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        set1 = Counter(nums1)\n        set2 = Counter(nums2)\n        N1 = len(nums1)\n        N2 = len(nums2)\n        n1sq = [n*n for n in nums1]\n        n2sq = [n*n for n in nums2]\n        ntr = 0\n        for i1 in range(N1): \n            for i2 in range(N2): \n                if n1sq[i1] % nums2[i2] == 0: \n                    if nums1[i1] != nums2[i2] and (n1sq[i1]//nums2[i2]) in set2: \n                        ntr += 0.5*set2[n1sq[i1]//nums2[i2]]\n                    elif nums1[i1] == nums2[i2]: \n                        ntr += 0.5*(set2[nums2[i2]] - 1)\n                if n2sq[i2] % nums1[i1] == 0: \n                    if nums2[i2] != nums1[i1] and (n2sq[i2]//nums1[i1]) in set1: \n                        ntr += 0.5*set1[n2sq[i2]//nums1[i1]]\n                    elif nums2[i2] == nums1[i1]: \n                        ntr += 0.5*(set1[nums1[i1]]-1)\n        return round(ntr)\n        \n", "class Solution:\n    def productPresent(self, numbers , required):\n        result = 0\n        count = collections.defaultdict(int)\n        for number in numbers:\n            count[number] += 1\n        \n        for number in numbers:\n            if required % number != 0:\n                continue\n            keyRequired = required // number\n            if keyRequired in count and count[keyRequired] > 0:\n                if keyRequired == number:\n                    result += (count[keyRequired] - 1)\n                else:\n                    result += count[keyRequired]\n            count[number] -= 1\n        \n        return result\n                    \n                    \n        \n        \n            \n            \n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        result = 0\n        for i in (nums1):\n            required = i * i\n            result += self.productPresent(nums2,required)\n        \n        for i in (nums2):\n            required = i * i\n            result += self.productPresent(nums1,required)\n        \n        return result\n        \n        \n", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        ret1 = self.check(nums1, nums2)\n        ret2 = self.check(nums2, nums1)\n        \n        return (ret1 + ret2)\n\n\n    def check(self, n1 :List[int], n2 :List[int]):\n        cnt = 0\n\n        for i in range(len(n1)):\n            square = n1[i]**2\n            count_n2 = Counter(n2)\n\n            for j in range(len(n2)):\n                if (square % n2[j] == 0):\n                    target = square // n2[j]\n\n                    if (count_n2[target] > 0):\n                        count_n2[n2[j]] -= 1\n                        cnt += count_n2[target]\n        \n        \n        return cnt\n", "class Solution:\n    def count(self, nums1: List[int], nums2: List[int]) -> int:\n        c = 0\n        for n in nums1:\n            sq = n ** 2\n            counter = Counter()\n            for k in nums2:\n                if sq % k == 0:\n                    c += counter[sq // k]\n                counter[k] += 1\n        return c\n\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        return self.count(nums1, nums2) + self.count(nums2, nums1)", "from collections import defaultdict\nclass Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        ret = 0\n        \n        h1 = defaultdict(int)\n        h2 = defaultdict(int)\n        for x in nums1:\n            h1[x] += 1\n        for x in nums2:\n            h2[x] += 1\n        \n        for x in nums1:\n            for y in nums2:\n                if x*x/y != x*x//y:\n                    continue\n                key = x*x//y\n                if h2[key] > 0:\n                    if key==y:\n                        ret += h2[key]-1\n                    else:\n                        ret += h2[key]\n        \n        for x in nums2:\n            for y in nums1:\n                if x*x/y != x*x//y:\n                    continue\n                key = x*x//y\n                if h1[key] > 0:\n                    if key==y:\n                        ret += h1[key]-1\n                    else:\n                        ret += h1[key]\n                        \n        return ret//2\n", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        ans = 0\n        def helper(l1,l2):\n            nonlocal ans\n            c = Counter(l2)\n            for x in l1:\n                num = x*x\n                c2 = c.copy()\n                for i in range(len(l2)):\n                    curr = l2[i]\n                    c2[curr] -= 1\n                    # if c2[curr] == 0:\n                    #     c2.pop(curr)\n                    # print(num,curr,c2)\n                    if num%curr == 0 and num//l2[i] in c2:\n                        ans += c2[num//l2[i]]\n                        \n        helper(nums1,nums2)\n        helper(nums2,nums1)\n        return ans\n        \n                    \n", "from collections import defaultdict\nclass Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        jk1, jk2 = defaultdict(set), defaultdict(set)\n        for j in range(len(nums1)):\n            for k in range(j + 1, len(nums1)):\n                jk1[nums1[j] * nums1[k]].add((j, k))\n        for j in range(len(nums2)):\n            for k in range(j + 1, len(nums2)):\n                jk2[nums2[j] * nums2[k]].add((j, k))\n        \n        \n        res = 0\n        for i in range(len(nums1)):\n            res += len(jk2[nums1[i] ** 2])\n        for i in range(len(nums2)):\n            res += len(jk1[nums2[i] ** 2])\n        \n        return res", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        def getRes(arr1, arr2):\n            count = 0\n            n = len(arr2)\n            max2 = arr2[-1]\n            for i in range(len(arr1)):\n                if arr1[i] > max2: break\n                target = arr1[i] ** 2\n                \n                seen = collections.defaultdict(int)\n                for j in range(n):\n                    if target / arr2[j] in seen:\n                        count += seen[target / arr2[j]]\n                    seen[arr2[j]] += 1\n                        \n            \n            return count\n        nums1.sort()\n        nums2.sort()\n        return getRes(nums1, nums2) + getRes(nums2, nums1)", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        ret = 0\n        m, n = len(nums1), len(nums2)\n        _map1, _map2 = Counter(nums1), Counter(nums2)\n        for i in range(m):\n            nums2_map = {**_map2}\n            for j in range(n):\n                nums2_map[nums2[j]] -= 1\n                target = nums1[i] * nums1[i] / nums2[j]\n                if nums2_map.get(target, 0) > 0:\n                    ret += nums2_map.get(target, 0)\n    \n        for i in range(n):\n            nums1_map = {**_map1}\n            for j in range(m):\n                nums1_map[nums1[j]] -= 1\n                target = nums2[i] * nums2[i] / nums1[j]\n                if nums1_map.get(target, 0) > 0:\n                    ret += nums1_map.get(target, 0)\n        \n        return ret", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        ans = 0\n        d = {}\n        drev = {}\n        \n        for i,x in enumerate(nums2):\n            if x in drev:\n                drev[x].add(i)\n            else:\n                drev[x] = {i}\n                \n        \n        # print(d)\n        \n        \n        for idx1,x in enumerate(nums1):\n            i2 = x*x\n            d = drev.copy()\n            drev.clear()\n            for idx,j in enumerate(nums2):\n                d[j].remove(idx)\n                if j in drev:drev[j].add(idx)\n                else: drev[j] = {idx}\n                \n                if i2%j == 0:\n                    k = i2//j\n                    if k in d:\n                        ans+=len(d[k])                                \n                                \n                                \n                                \n        d = {}\n        drev = {}\n        \n        for i,x in enumerate(nums1):\n            if x in drev:\n                drev[x].add(i)\n            else:\n                drev[x] = {i}\n                \n        \n        # print(d)\n        \n        \n        for idx1,x in enumerate(nums2):\n            i2 = x*x\n            d = drev.copy()\n            drev.clear()\n            for idx,j in enumerate(nums1):\n                d[j].remove(idx)\n                if j in drev:drev[j].add(idx)\n                else: drev[j] = {idx}\n                \n                if i2%j == 0:\n                    k = i2//j\n                    if k in d:\n                        ans+=len(d[k])           \n        \n                                \n                                \n        return ans\n                                \n    \n                    \n                \n", "class Solution:\n    def numTriplets(self, a: List[int], b: List[int]) -> int:\n        map1 = {}\n        map2 = {}\n\n        for i in range(len(a)):\n            for j in range(len(a)):\n                if i!=j:\n                    if a[i]*a[j] not in map1.keys():\n                        map1[a[i]*a[j]]= 0\n                    map1[a[i]*a[j]]+=1\n\n        for i in range(len(b)):\n              for j in range(len(b)):\n                if i!=j:\n                    if b[i]*b[j] not in map2.keys():\n                        map2[b[i]*b[j]]= 0\n                    map2[b[i]*b[j]]+=1\n\n        count = 0\n        for i in a:\n            if i*i in map2.keys():\n                count+=map2[i*i]\n\n        for i in b:\n            if i*i in map1.keys():\n                count+=map1[i*i]\n                \n        return count//2", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        d1 = collections.Counter(nums1)\n        d2 = collections.Counter(nums2)        \n        ans = 0\n        for i in range(len(nums1)):\n            for j in range(i+1,len(nums1)):\n                p = nums1[i]*nums1[j]\n                if (int(math.sqrt(p)))**2 == p and int(math.sqrt(p)) in d2:\n                    ans+=d2[int(math.sqrt(p))]\n        for i in range(len(nums2)):\n            for j in range(i+1,len(nums2)):\n                p = nums2[i]*nums2[j]\n                if (int(math.sqrt(p)))**2 == p and int(math.sqrt(p)) in d1:\n                    ans+=d1[int(math.sqrt(p))]\n        return ans\n", "class Solution:\n    def numTriplets(self, A: List[int], B: List[int]) -> int:\n        def find(X,Y):\n            res = 0\n            for x in X:\n                have = collections.Counter(Y)\n                for i, y in enumerate(Y):\n                    have[y] -= 1\n                    if x*x % y == 0:  \n                        res += have[x*x//y]\n            return res\n        return find(A,B) + find(B, A)\n        \n", "class Solution:\n    \n    def all_products(self, int_list):\n        res_list = []\n        for idx,i in enumerate(int_list):\n            for j in int_list[idx+1:]:\n                res_list.append(i*j)\n        return res_list\n    \n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        res = 0\n        squared_nums1 = [i**2 for i in nums1]\n        squared_nums2 = [i**2 for i in nums2]\n        products_nums1 = self.all_products(nums1)\n        products_nums2 = self.all_products(nums2)\n        # print (squared_nums1, squared_nums2, products_nums1, products_nums2)\n        for i in set(squared_nums2):\n            c1 = squared_nums2.count(i)\n            c2 = products_nums1.count(i)\n            res += c1 * c2\n        for j in set(squared_nums1):\n            d1 = squared_nums1.count(j)\n            d2 = products_nums2.count(j)\n            res += d1 * d2\n        return res", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        nums1 = list(Counter(nums1).items())\n        nums2 = list(Counter(nums2).items())\n        def find(target, count1, arr):\n            seen = Counter()\n            ans = 0\n            for a, count2 in arr:\n                if target % a == 0:\n                    if (b := target // a) != a:\n                        ans += count1 * count2 * seen[b]\n                    else:\n                        ans += count1 * math.comb(count2, 2)\n                seen[a] += count2\n            return ans\n\n        return sum(find(a*a, c, nums2) for a, c in nums1) + sum(find(a*a, c, nums1) for a, c in nums2)", "class Solution:\n    \n    \n    \n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        \n        self.count = 0\n        \n        def computeCount(nums1, nums2):\n            hashMap = {}\n            for i in range(len(nums1)):\n                checkNum = nums1[i] ** 2\n                for j in range(len(nums2)):\n                    modulus = checkNum/nums2[j]\n                    if modulus in hashMap:\n                        self.count += hashMap[modulus]\n                    if nums2[j] in hashMap:\n                        hashMap[nums2[j]] += 1\n                    else:\n                        hashMap[nums2[j]] = 1             \n                hashMap.clear()\n\n        computeCount(nums1, nums2)\n        computeCount(nums2,nums1)\n        return self.count", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        \n        def twoProduct(target, nums) -> int:\n            goal = defaultdict(list)\n            tot = 0\n            for i in range(len(nums)):\n                if len(goal[nums[i]]) != 0:\n                    tot += len(goal[nums[i]])\n                goal[target/(nums[i])].append(i)\n            \n            return tot\n        \n        count = 0\n        for n in nums1:\n            count += twoProduct(n*n, nums2)\n        \n        for n in nums2:\n            count += twoProduct(n*n, nums1)\n        \n        return count", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        one = [i**2 for i in nums1]\n        two = [j**2 for j in nums2]\n        self.cnt = 0\n        d1 = collections.defaultdict(int)\n        self.calculate(one,nums2)\n        self.calculate(two,nums1)\n        return self.cnt\n    def calculate(self,square,nums):\n        for i in range(len(square)):\n            target = square[i]\n            d = collections.defaultdict(list)\n            for j in range(len(nums)):\n                if target%nums[j]==0 and int(target/nums[j]) in d:\n                    # print(target,nums[j],d[nums[j]])\n                    self.cnt+=len(d[target/nums[j]])\n                d[nums[j]].append(j)\n                # print(target,d)\n", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        \n        def helper(a, b):\n            res = 0\n            for num in a:\n                tar = num ** 2\n                cnt = collections.defaultdict(int)\n                for x in b:\n                    res += cnt[tar / x]\n                    cnt[x] += 1\n            return res\n    \n        return helper(nums1, nums2) + helper(nums2, nums1)", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        if not nums1 or not nums2:\n            return 0\n        \n        return self.helper(nums1, nums2) + self.helper(nums2, nums1)\n    \n    def helper(self, nums1, nums2):\n        lookup = {}\n        count = 0\n        for num2 in nums2:\n            for num1 in nums1:\n                square = num1**2\n                mod = square%num2\n                divide = square//num2\n                if square >= num2 and mod == 0 and divide in lookup:\n                    count += lookup[divide]\n            if num2 not in lookup:\n                lookup[num2] = 0\n            lookup[num2] += 1\n        return count", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        '''Input: nums1 = [7,4], nums2 = [5,2,8,9]\n        Output: 1\n        Explanation: Type 1: (1,1,2), nums1[1]^2 = nums2[1] * nums2[2]. (4^2 = 2 * 8). \n        '''\n        if not nums1 or not nums2:\n            return 0\n        \n        res = 0\n        newlist1 = collections.defaultdict(set)\n        newlist2 = collections.defaultdict(set)\n        \n        \n        for i in range(len(nums2)-1):\n            for j in range(i+1, len(nums2)):\n                key = nums2[i]*nums2[j]\n                newlist1[key].add((i, j))\n                \n        for i in range(len(nums1)-1):\n            for j in range(i+1, len(nums1)):\n                key = nums1[i]*nums1[j]\n                newlist2[key].add((i, j))\n        # print(newlist)\n        \n        for n in nums1:\n            if n**2 in newlist1:\n                res += len(newlist1[n**2])\n        for n in nums2:\n            if n**2 in newlist2:\n                res += len(newlist2[n**2])\n        return res\n", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        table1 = {}\n        table2 = {}\n        res = 0\n        \n        for n in nums1:\n            square = n * n\n            table = {}\n            for j, m in enumerate(nums2):\n                if square % m == 0:\n                    remainder = square // m\n                    if remainder in table:\n                        # print(square, m, remainder, table)\n                        res += table[remainder]\n                if m not in table:\n                    table[m] = 0\n                table[m] += 1\n                \n        for n in nums2:\n            square = n * n\n            table = {}\n            for j, m in enumerate(nums1):\n                if square % m == 0:\n                    remainder = square // m\n                    if remainder in table:\n                        # print(square, m, remainder, table)\n\n                        res += table[remainder]\n                if m not in table:\n                    table[m] = 0\n                table[m] += 1  \n        return res\n", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        nums1Counter, nums2Counter, result = Counter(nums1), Counter(nums2), 0\n        \n        def isPerfectSquare(n):\n            squareRoot = math.sqrt(n)\n            return {\n                'status': squareRoot - floor(squareRoot) == 0,\n                'value': int(floor(squareRoot))\n            }\n        \n        for num2j, num2k in combinations(nums2, 2):\n            perfectSquare = isPerfectSquare(num2j * num2k)\n            if perfectSquare['status']:\n                result += nums1Counter[perfectSquare['value']]\n        for num1j, num1k in combinations(nums1, 2):\n            perfectSquare = isPerfectSquare(num1j * num1k)\n            if perfectSquare['status']:\n                result += nums2Counter[perfectSquare['value']]\n        return result", "from itertools import combinations\nclass Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        sq1, sq2 = [num ** 2 for num in nums1], [num ** 2 for num in nums2]\n        prod1, prod2 = [combo[0] * combo[1] for combo in combinations(nums1, 2)], [combo[0] * combo[1] for combo in combinations(nums2, 2)]\n        result = 0\n        for sq in set(sq1):\n            result += sq1.count(sq) * prod2.count(sq)\n        for sq in set(sq2):\n            result += sq2.count(sq) * prod1.count(sq)\n        return result", "from collections import defaultdict\nfrom typing import List\n\n\nclass Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        # \u30cf\u30c3\u30b7\u30e5\u5316\n        h1 = {}\n        h2 = {}\n\n        for n in nums1:\n            if n in h1:\n                h1[n] += 1\n            else:\n                h1[n] = 1\n        for n in nums2:\n            if n in h2:\n                h2[n] += 1\n            else:\n                h2[n] = 1\n\n        count = 0\n\n        # Type1\n        for n in nums1:\n            n2 = n * n\n            for v in list(h2.keys()):\n                q, m = divmod(n2, v)\n                if m != 0 or q not in h2:\n                    continue\n\n                l1 = h2[v]\n                if v == q:\n                    if l1 > 1:\n                        # l1\u500b\u306e\u4e2d\u304b\u30892\u500b\u9078\u3076\u7d44\u307f\u5408\u308f\u305b\u306e\u6570\n                        count += (l1 * (l1-1))\n                else:\n                    l2 = h2[q]\n                    count += l1 * l2\n\n        # Type2\n        for n in nums2:\n            n2 = n * n\n            for v in list(h1.keys()):\n                q, m = divmod(n2, v)\n                if m != 0 or q not in h1:\n                    continue\n\n                l1 = h1[v]\n                if v == q:\n                    if l1 > 1:\n                        # l1\u500b\u306e\u4e2d\u304b\u30892\u500b\u9078\u3076\u7d44\u307f\u5408\u308f\u305b\u306e\u6570\n                        count += (l1 * (l1-1))\n                else:\n                    l2 = h1[q]\n                    count += l1 * l2\n\n        return int(count / 2)\n", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        result = 0\n        for i in range(len(nums1)):\n            dic = collections.defaultdict(int)\n            for j in range(len(nums2)):\n                target = (nums1[i] **2) / nums2[j]\n                if target in dic:\n                    result += dic[target]\n                dic[nums2[j]] += 1\n                \n        for i in range(len(nums2)):\n            dic = collections.defaultdict(int)\n            for j in range(len(nums1)):\n                target = nums2[i] **2 / nums1[j]\n                if target in dic:\n                    result += dic[target]\n                dic[nums1[j]] += 1\n        return result", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:     \n        res = 0\n        res += sum(self.get_target_products(n * n, nums2) for n in nums1)\n        res += sum(self.get_target_products(n * n, nums1) for n in nums2)\n        return res\n    \n    def get_target_products(self, target, nums):\n        d = collections.Counter()\n        res = 0\n        for i in range(len(nums)):\n            if target / nums[i] in d:\n                res += d[target / nums[i]]\n            d[nums[i]] += 1\n        return res\n", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        # nums1.sort()\n        # nums2.sort()\n        count=0\n        for i in range(len(nums1)):\n            target=nums1[i]**2\n            temp=defaultdict(int)\n            for j in range(len(nums2)):\n                if (target/nums2[j]) in list(temp.keys()):\n                    count+=temp[target/nums2[j]]\n                temp[nums2[j]]+=1\n                        \n        for i in range(len(nums2)):\n            target=nums2[i]**2\n            temp=defaultdict(int)\n            for j in range(len(nums1)):\n                if (target/nums1[j]) in list(temp.keys()):\n                    count+=temp[target/nums1[j]]\n                temp[nums1[j]]+=1\n        # for i in range(len(nums1)):\n        #     s2=nums1[i]**2\n        #     first,last=0,len(nums2)-1\n        #     while nums2[last//2]>s2:\n        #         last//=2+1\n        #     while first<last:\n        #         if nums2[first]*nums2[last]==s2:\n        #             count+=1\n        #             break\n        #         elif nums2[first]*nums2[last]>s2:\n        #             last-=1\n        #         else:\n        #             first+=1\n        # for i in range(len(nums2)):\n        #     s2=nums2[i]**2\n        #     first,last=0,len(nums1)-1\n        #     while nums1[last//2]>s2:\n        #         last//=2+1\n        #     while first<last:\n        #         if nums1[first]*nums1[last]==s2:\n        #             count+=1\n        #             break\n        #         elif nums1[first]*nums1[last]>s2:\n        #             last-=1\n        #         else:\n        #             first+=1\n        return count\n", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        # brute force?\n        nums1.sort()\n        nums2.sort()\n        \n        # type 1\n        memo1 = collections.defaultdict(set)\n        memo2 = collections.defaultdict(set)\n        for i in range(len(nums1)):\n            memo1[nums1[i]].add(i)\n        for i in range(len(nums2)):\n            memo2[nums2[i]].add(i)\n        \n        cnt = 0\n        for i in range(len(nums1)):\n            sq = nums1[i]*nums1[i]\n            for j in range(len(nums2)):\n                if nums2[j] > sq: break\n                if sq % nums2[j] != 0:\n                    continue\n                target = sq//nums2[j]\n                if target in memo2:\n                    cnt += len(memo2[target])\n                    if j in memo2[target]:\n                        cnt -= 1 # self\n        for i in range(len(nums2)):\n            sq = nums2[i]*nums2[i]\n            for j in range(len(nums1)):\n                if nums1[j] > sq: break\n                if sq % nums1[j] != 0:\n                    continue\n                target = sq//nums1[j]\n                if target in memo1:\n                    cnt += len(memo1[target])\n                    if j in memo1[target]:\n                        cnt -= 1 # self\n        return cnt // 2", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        result = 0\n        for i in range(len(nums1)):\n            dic = collections.defaultdict(int)\n            for j in range(len(nums2)):\n                target = (nums1[i] ** 2) / nums2[j]\n                if target in dic:\n                    result += dic[target]\n                dic[nums2[j]] += 1\n        for i in range(len(nums2)):\n            dic = collections.defaultdict(int)\n            for j in range(len(nums1)):\n                target = (nums2[i] ** 2) / nums1[j]\n                if target in dic:\n                    result += dic[target]\n                dic[nums1[j]] += 1\n        return result", "from collections import Counter\n\nclass Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        counter1 = Counter(nums1)\n        counter2 = Counter(nums2)\n\n        ret = 0\n        for k, v in counter1.items():\n            for i in range(1, k):\n                if i in counter2 and k*k/i in counter2:\n                    ret += v * counter2[i] * counter2[k*k/i]\n            if counter2[k] >= 2:\n                ret += v * counter2[k]*(counter2[k]-1)/2\n\n        for k, v in counter2.items():\n            for i in range(1, k):\n                if i in counter1 and k*k/i in counter1:\n                    ret += v * counter1[i] * counter1[k*k/i]\n            if counter1[k] >= 2:\n                ret += v * counter1[k]*(counter1[k]-1)/2\n        return int(ret)", "from collections import Counter\n\nclass Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        nums1_products = productCounts(nums1)\n        nums2_products = productCounts(nums2)\n        nums1_squares = squareCounts(nums1)\n        nums2_squares = squareCounts(nums2)\n\n        result = 0\n        for square, count in nums1_squares.items():\n            result += count * nums2_products[square]\n\n        for square, count in nums2_squares.items():\n            result += count * nums1_products[square]\n\n        return result\n\ndef productCounts(arr):\n    result = Counter()\n    for i in range(len(arr)):\n        for j in range(i + 1, len(arr)):\n            result.update((arr[i] * arr[j],))\n    return result\n\ndef squareCounts(arr):\n    result = Counter()\n    for num in arr:\n        result.update((num ** 2,))\n    return result", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        nums1 = list(Counter(nums1).items())\n        nums2 = list(Counter(nums2).items())\n        def find(target, count1, arr):\n            seen = Counter()\n            ans = 0\n            for a, count2 in arr:\n                if target % a == 0:\n                    if (b := target // a) != a:\n                        ans += count1 * count2 * seen[b]\n                    else:\n                        ans += count1 * math.comb(count2, 2)\n                seen[a] += count2\n            return ans\n            \n        @lru_cache(None)\n        def twoProduct1(target, count):\n            return find(target, count, nums1)\n\n        @lru_cache(None)\n        def twoProduct2(target, count):\n            return find(target, count, nums2)\n\n        return sum(twoProduct2(a*a, c) for a, c in nums1) + sum(twoProduct1(a*a, c) for a, c in nums2)", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        # counters\n        nums1_squared = [i**2 for i in nums1]\n        nums2_squared = [i**2 for i in nums2]\n        \n        c1 = collections.Counter(nums1)\n        c2 = collections.Counter(nums2)\n        \n        res = 0\n        for n1 in nums1:\n            for target in nums2_squared:\n                if target/n1 in c1: \n                    res += c1[target/n1]\n                    if n1**2 == target: res -= 1\n        \n        for n2 in nums2:\n            for target in nums1_squared:\n                if target/n2 in c2:\n                    res += c2[target/n2]\n                    if n2**2 == target: res -= 1\n        \n        return res//2\n", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        from collections import Counter\n        set1=Counter(nums1)\n        set2=Counter(nums2)\n        ret=0\n        for i in range(len(nums1)):\n            for j in range(i+1,len(nums1)):\n                temp=int(math.sqrt(nums1[i]*nums1[j]))\n                if temp**2==nums1[i]*nums1[j] and temp in set2:\n                    ret+=set2[temp]\n        for i in range(len(nums2)):\n            for j in range(i+1,len(nums2)):\n                temp=int(math.sqrt(nums2[i]*nums2[j]))\n                if temp**2==nums2[i]*nums2[j] and temp in set1:\n                    ret+=set1[temp]\n        return ret", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        def solve(n1s, n2s):\n            answer = 0\n            for num1 in n1s:\n                m = collections.defaultdict(int)\n                for num2 in n2s:\n                    if num1**2 % num2 == 0:\n                        answer += m[num1**2//num2]\n                    m[num2] += 1\n            return answer\n        return solve(nums1, nums2)+solve(nums2,nums1)", "from typing import List\nimport bisect\nfrom math import comb\n\n\nclass Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        res = 0\n        nums1.sort()\n        nums2.sort()\n        res += self.find(nums1, nums2)\n        res += self.find(nums2, nums1)\n        return res\n\n    def find(self, n1, n2):\n        res = 0\n        for n in n1:\n            left = 0\n            right = len(n2) - 1\n            while left < right:\n                if n2[left] * n2[right] == n ** 2:\n                    if n2[left] == n2[right]:\n                        res += comb(right - left + 1, 2)\n                        while left < right and n2[left] == n2[left + 1]:\n                            left += 1\n                        while left < right and n2[right] == n2[right - 1]:\n                            right -= 1\n                    else:\n                        nl = 1\n                        nr = 1\n                        while left < right and n2[left] == n2[left + 1]:\n                            nl += 1\n                            left += 1\n                        while left < right and n2[right] == n2[right - 1]:\n                            nr += 1\n                            right -= 1\n                        res += nl * nr\n                    left += 1\n                    right -= 1\n\n                elif n2[left] * n2[right] < n ** 2:\n                    left += 1\n                else:\n                    right -= 1\n        return res\n", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        # all positive numbers\n        \n#         nums1.sort()\n#         nums2.sort()\n        \n#         d1 = {}\n#         d2 = {}\n        \n#         self.ret = 0\n#         def helper(a, b):\n#             # pick one from a and pick two from b, record products in d\n#             if len(b) < 2:\n#                 return\n#             # print(a, b)\n#             for n in a:\n#                 # iterate through a\n#                 # n ^ 2 = product of two numbers in b\n#                 for i in range(len(b)-1):\n#                     if n * n > b[i] * b[-1]:\n#                         continue\n#                     for j in range(i+1, len(b)):\n#                         if n * n == b[i] * b[j]:\n#                             # found pairs\n#                             self.ret += 1\n#                         elif n * n < b[i] * b[j]:\n#                             break\n                            \n                            \n#         helper(nums1, nums2)\n#         helper(nums2, nums1)\n                            \n        # return self.ret\n        \n        \n        \n        \n        def helper(target, nums):\n            ret = 0\n            target = target * target\n            d = {}\n            s = set()\n            for n in nums:\n                if n in d:\n                    # there is a pair\n                    s.add((min(n, target // n), max(n, target // n)))\n                \n                if target % n == 0:\n                    # candidate\n                    if target // n in d:\n                        d[target // n] += 1\n                    else:\n                        d[target // n] = 1\n            \n            # print(target, nums, s, d)\n            for a, b in s:\n                if a == b:\n                    ret += d[a] * (d[a]-1) // 2\n                else:    \n                    ret += d[a] * d[b]\n            return ret\n           \n        ans = 0\n        for n1 in nums1:\n            ans += helper(n1, nums2)\n        for n2 in nums2:\n            ans += helper(n2, nums1)\n                    \n        return ans\n        \n", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        d1, d2 = dict(), dict()\n\n        for idx, n1 in enumerate(nums1):\n            d1[idx] = dict()\n            for elm in nums1[idx+1:]:\n                try:\n                    d1[idx][elm] += 1\n                except KeyError:\n                    d1[idx][elm] = 1\n\n\n        for idx, n2 in enumerate(nums2):\n            d2[idx] = dict()\n            for elm in nums2[idx+1:]:\n                try:\n                    d2[idx][elm] += 1\n                except KeyError:\n                    d2[idx][elm] = 1\n                    \n        good = 0\n\n        for n1 in nums1:\n            for idx, n2 in enumerate(nums2):\n                try:\n                    good += d2[idx][n1**2/n2]\n                except KeyError:\n                    pass\n\n\n\n\n        for n2 in nums2:\n            for idx, n1 in enumerate(nums1):\n                try:\n                    good += d1[idx][n2**2/n1] \n                except KeyError:\n                    pass\n        \n        return good\n        \n", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        n1 = len(nums1)\n        n2 = len(nums2)\n\n        nn1 = []\n        nn2 = []\n        \n        for j in range(n2):\n            for k in range(j+1,n2):\n                nn2.append(nums2[j]*nums2[k])\n        for j in range(n1):\n            for k in range(j+1,n1):\n                nn1.append(nums1[j]*nums1[k])\n        \n        cnt = 0\n        nnn1 = set(nums1)\n        nnn2 = set(nums2)\n        for i in nnn1:\n            cnt+= nn2.count(i*i)*nums1.count(i)\n        \n        for i in nnn2:\n            cnt+= nn1.count(i*i)*nums2.count(i)\n        \n        return cnt", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        def count(num, nums):\n            m = {}\n            cnt = 0\n            for n in nums:\n                if num%n == 0:\n                    if m.get(num/n) is not None:\n                        cnt += m[num/n]\n                if m.get(n) is None:\n                    m[n] = 1\n                else:\n                    m[n] += 1\n            return cnt\n\n        res = 0\n        for num in nums1:\n            res += count(num*num, nums2)\n        for num in nums2:\n            res += count(num*num, nums1)\n        return res", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        ans = 0\n        def helper(l1, l2):\n            nonlocal ans\n            for e in l1:\n                tar = e**2\n                dp = Counter()\n                for x in l2:\n                    if tar%x==0:\n                        ans += dp[tar//x]\n                    dp[x] += 1\n        helper(nums1, nums2)\n        helper(nums2, nums1)\n        return ans", "from collections import defaultdict\nclass Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        dic1 = defaultdict(list)\n        dic2 = defaultdict(list)\n        for idx, i in enumerate(nums1):\n            dic1[i].append(idx)\n        for idx, i in enumerate(nums2):\n            dic2[i].append(idx)\n        count = 0\n        for i in nums1:\n            sq = i * i\n            for j in set(nums2):\n                ano = sq / j\n                # print(\\\"ano,\\\", ano, sq, j)\n                if ano != int(ano): continue\n                ano = int(ano)\n                if ano in dic2:\n                    inc = 0\n                    if ano == j:\n                        inc += int(len(dic2[ano]) * (len(dic2[ano])-1) / 2)\n                    else:\n                        inc += len(dic2[ano]) * len(dic2[j]) / 2\n                    count += inc\n                    # print(\\\"i is\\\", i, \\\" inc\\\", inc)\n        for i in nums2:\n            sq = i * i\n            for j in set(nums1):\n                ano = sq / j\n                if ano != int(ano): continue\n                ano = int(ano)\n                if ano in dic1:\n                    inc = 0\n                    if ano == j:\n                        inc += int(len(dic1[ano]) * (len(dic1[ano])-1) / 2)\n                    else:\n                        inc += len(dic1[ano]) * len(dic1[j]) / 2\n                    # print(\\\"i is\\\", i, \\\" inc\\\", inc)\n                    count += inc\n        return int(count)", "import math\nclass Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        def isSquare(n):\n            return math.sqrt(n) == int(math.sqrt(n))\n        from collections import Counter\n        hash1 = Counter(nums1)\n        print(hash1)\n        out = 0\n        \n        for i in range(len(nums2)):\n            for j in range(i+1, len(nums2)):\n                k = nums2[i] * nums2[j]\n                if isSquare(k):\n                    if int(math.sqrt(k)) in hash1:\n                        out += hash1[int(math.sqrt(k))]\n        print(out)\n        hash2 = Counter(nums2)\n        \n        for i in range(len(nums1)):\n            for j in range(i+1, len(nums1)):\n                k = nums1[i] * nums1[j]\n                if isSquare(k):\n                    if int(math.sqrt(k)) in hash2:\n                        out += hash2[int(math.sqrt(k))]\n        print(out)\n        return out", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        d1, d2 = dict(), dict()\n\n        for idx, n1 in enumerate(nums1):\n            d1[idx] = dict()\n            for elm in nums1[idx+1:]:\n                try:\n                    d1[idx][elm] += 1\n                except KeyError:\n                    d1[idx][elm] = 1\n\n        for idx, n2 in enumerate(nums2):\n            d2[idx] = dict()\n            for elm in nums2[idx+1:]:\n                try:\n                    d2[idx][elm] += 1\n                except KeyError:\n                    d2[idx][elm] = 1\n                    \n        good = 0\n\n        for n1 in nums1:\n            for idx, n2 in enumerate(nums2):\n                try:\n                    good += d2[idx][n1**2/n2]\n                except KeyError:\n                    pass\n\n\n\n\n        for n2 in nums2:\n            for idx, n1 in enumerate(nums1):\n                try:\n                    good += d1[idx][n2**2/n1] \n                except KeyError:\n                    pass\n        \n        return good\n        \n", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        ans = 0\n        # nums1.sort()\n        # nums2.sort()\n        for i in range(len(nums1)):\n            hashmap = collections.defaultdict(int)\n            for j in range(len(nums2)):\n                if nums1[i] * nums1[i] / nums2[j] in hashmap:\n                    ans += hashmap[nums1[i] * nums1[i] / nums2[j]]\n                    # print(i,j, hashmap)\n                hashmap[nums2[j]] += 1\n                \n        for i in range(len(nums2)):\n            hashmap = collections.defaultdict(int)\n            for j in range(len(nums1)):\n                if nums2[i] * nums2[i] / nums1[j] in hashmap:\n                    ans += hashmap[nums2[i] * nums2[i] / nums1[j]]\n                    # print(i,j, hashmap)\n                hashmap[nums1[j]] += 1\n            \n            # for j in range(len(nums2) - 1):\n            #     k = j + 1\n            #     while k < len(nums2):\n            #         if nums1[i] * nums1[i] == nums2[j] * nums2[k]:\n            #             ans += 1\n            #             # print((i,j,k))\n        #     #         k += 1\n        # for i in range(len(nums2)):\n        #     j, k = 0, len(nums1) - 1\n        #     while j < k:\n        #         if nums2[i] * nums2[i] == nums1[j] * nums1[k]:\n        #             ans += 1\n        #             j += 1\n        #         elif nums2[i] * nums2[i] < nums1[j] * nums1[k]:\n        #             k -= 1\n        #         else:\n        #             j += 1\n        return ans\n", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        ans = 0\n        def helper(l1,l2):\n            nonlocal ans\n            c = Counter(l2)\n            for x in l1:\n                num = x*x\n                c2 = c.copy()\n                for i in range(len(l2)):\n                    curr = l2[i]\n                    c2[curr] -= 1\n                    if c2[curr] == 0:\n                        c2.pop(curr)\n                    # print(num,curr,c2)\n                    if num%curr == 0 and num//l2[i] in c2:\n                        ans += c2[num//l2[i]]\n                        \n        helper(nums1,nums2)\n        helper(nums2,nums1)\n        return ans\n        \n                    \n", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        ans = 0\n        for n in nums1:\n            d = {}\n            for i,m in enumerate(nums2):\n                if n**2/m in d:\n                    ans+=d[n**2/m]\n                if m in d:\n                    d[m]+=1\n                else:\n                    d[m]=1\n        for n in nums2:\n            d = {}\n            for i,m in enumerate(nums1):\n                if n**2/m in d:\n                    ans+=d[n**2/m]\n                if m in d:\n                    d[m]+=1\n                else:\n                    d[m]=1\n        return ans\n                \n", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        nums1.sort()\n        nums2.sort()\n        def ok(n, sorted_lst):\n            i,j = 0, len(sorted_lst)-1\n            ret = 0\n            #print(n, sorted_lst)\n            while 0<=i<j<len(sorted_lst):\n                t = sorted_lst[i]*sorted_lst[j]\n                if t>n:\n                    j-=1\n                    while i<j<len(sorted_lst)-1 and sorted_lst[j] == sorted_lst[j+1]:\n                        j-=1\n                elif t<n:\n                    i+=1\n                    while 0<i<j and sorted_lst[i]==sorted_lst[i-1] :\n                        i+=1\n                elif sorted_lst[i]==sorted_lst[j]: #reach center elements\n                    #print(n, sorted_lst[i])\n                    ret += (j-i+1)*(j-i)//2 \n                    break\n                else:\n                    #print(n, i,j,sorted_lst[i],sorted_lst[j])\n                    a,b=1,1\n                    i+=1\n                    while 0<i<j and sorted_lst[i] == sorted_lst[i-1]:\n                        i+=1\n                        a+=1\n                    j-=1\n                    #print(i,j,sorted_lst[j])\n                    while i<=j<len(sorted_lst)-1 and sorted_lst[j] == sorted_lst[j+1]:\n                        j-=1\n                        b+=1\n                    #print(a,b, a*b)\n                    ret += a*b\n                    \n            return ret\n        \n        tot, cur = 0, 0\n        for i, v in enumerate(nums1):\n            if i==0 or v!=nums1[i-1]:\n                cur = ok(v*v,nums2)\n            tot += cur\n        \n        for i, v in enumerate(nums2):\n            if i==0 or v!=nums2[i-1]:\n                cur = ok(v*v,nums1)\n            tot+=cur\n        return tot", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        nums1_cnt = collections.Counter(nums1)\n        nums1_square_cnt = {x*x:nums1_cnt[x] for x in nums1_cnt}\n        nums2_cnt = collections.Counter(nums2)\n        nums2_square_cnt = {y*y:nums2_cnt[y] for y in nums2_cnt}\n\n        def f(nums_a, cnt_a, cnt_b_squares):\n            uniq_a_vals = list(cnt_a.keys())\n            if len(uniq_a_vals) == 1:\n                x = uniq_a_vals[0]\n                if x * x in cnt_b_squares:\n                    return (cnt_a[x] * (cnt_a[x] - 1) // 2) * cnt_b_squares[x*x]\n            \n            total = 0\n            for i in range(len(nums_a)):\n                for j in range(i + 1, len(nums_a)):\n                    x, y = nums_a[i], nums_a[j]\n                    if x * y in cnt_b_squares:\n                        total +=  cnt_b_squares[x*y]\n            return total\n        \n        return f(nums1, nums1_cnt, nums2_square_cnt) + f(nums2, nums2_cnt, nums1_square_cnt)", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        nums1_cnt = collections.Counter(nums1)\n        nums1_square_cnt = {x*x:nums1_cnt[x] for x in nums1_cnt}\n        nums2_cnt = collections.Counter(nums2)\n        nums2_square_cnt = {y*y:nums2_cnt[y] for y in nums2_cnt}\n\n        def f(nums_a, cnt_a, cnt_b_squares):\n            if len(cnt_a) == 1:\n                x = next(iter(cnt_a))\n                if x * x in cnt_b_squares:\n                    return (cnt_a[x] * (cnt_a[x] - 1) // 2) * cnt_b_squares[x*x]\n            \n            total = 0\n            for i in range(len(nums_a)):\n                for j in range(i + 1, len(nums_a)):\n                    x, y = nums_a[i], nums_a[j]\n                    if x * y in cnt_b_squares:\n                        total +=  cnt_b_squares[x*y]\n            return total\n        \n        return f(nums1, nums1_cnt, nums2_square_cnt) + f(nums2, nums2_cnt, nums1_square_cnt)", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        nums1_cnt = collections.Counter(nums1)\n        nums1_square_cnt = {x*x:nums1_cnt[x] for x in nums1_cnt}\n        nums2_cnt = collections.Counter(nums2)\n        nums2_square_cnt = {y*y:nums2_cnt[y] for y in nums2_cnt}\n\n        def f(nums_a, cnt_a, cnt_b_squares):\n            if len(cnt_a) == 1:\n                x = next(iter(cnt_a))\n                if x * x in cnt_b_squares:\n                    return (cnt_a[x] * (cnt_a[x] - 1) // 2) * cnt_b_squares[x*x]\n            \n            total = 0\n            for i in range(len(nums_a)):\n                for j in range(i + 1, len(nums_a)):\n                    x, y = nums_a[i], nums_a[j]\n                    if x * y in cnt_b_squares:\n                        total += cnt_b_squares[x*y]\n            return total\n        \n        return f(nums1, nums1_cnt, nums2_square_cnt) + f(nums2, nums2_cnt, nums1_square_cnt)", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        c1 = collections.Counter(nums1)\n        c2 = collections.Counter(nums2)\n        \n        res = 0\n        for x in c1:\n            for y in c2:\n                if x == y:\n                    res += (c1[x] * c2[y] * (c2[y] - 1))//2                  \n                elif x**2 % y == 0 and y < x**2 // y:\n                    res += c1[x]*c2[y]*c2[x**2 // y]                    \n        \n        c1, c2 = c2, c1\n        for x in c1:\n            for y in c2:\n                if x == y:\n                    res += (c1[x] * c2[y] * (c2[y] - 1))//2\n                elif x**2 % y == 0 and y < x**2 // y:\n                    res += c1[x]*c2[y]*c2[x**2 // y]\n                    \n        return res\n", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        def f(u, v):\n            v = Counter(float(i) for i in v)\n            # print(f'u {u} v {v}')\n            for x in u:\n                w = x * x\n                \n                for y in v:\n                    if y == x:\n                        n = v[y]\n                        # print(f'!! x {x} y {y} n {n}')\n                        yield n * (n-1)\n                        continue\n                    z = w / y\n                    if z in v:\n                        # print(f'!! x {x} y {y} z{z} v[y] * v[z] {v[y] * v[z]}')\n                        yield v[y] * v[z]\n        \n        # a = f(nums1, nums2)\n        # b = f(nums2, nums1)\n        # print(sum(a), sum(b))\n        a = sum(f(nums1, nums2)) + sum(f(nums2, nums1))\n        return a // 2", "from functools import lru_cache\nfrom collections import Counter\n\n\nclass Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        def find(target, arr):\n            seen = Counter()\n            count = 0\n            for a in arr:\n                if target % a == 0:\n                    count += seen[target // a]\n                seen[a] += 1\n            return count\n\n        @lru_cache(None)\n        def twoProduct1(target):\n            return find(target, nums1)\n\n        @lru_cache(None)\n        def twoProduct2(target):\n            return find(target, nums2)\n\n        return sum(twoProduct2(a * a) for a in nums1) + sum(twoProduct1(a * a) for a in nums2)", "from collections import Counter\n\n\nclass Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        def find(target, arr):\n            seen = Counter()\n            count = 0\n            for a in arr:\n                if target % a == 0:\n                    count += seen[target // a]\n                seen[a] += 1\n            return count\n\n        def twoProduct(target, nums):\n            if target in lut:\n                return lut[target]\n            lut[target] = find(target, nums)\n            return lut[target]\n\n        lut = Counter()\n        ret1 = sum(twoProduct(a * a, nums2) for a in nums1)\n        lut = Counter()\n        ret2 = sum(twoProduct(a * a, nums1) for a in nums2)\n        return ret1 + ret2", "from collections import Counter\n\nclass Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        count1 = Counter(nums1)\n        count2 = Counter(nums2)\n        return self.count_triplets(count1, count2) + self.count_triplets(count2, count1)\n        \n    def count_triplets(self, count1, count2):\n        triplets = 0\n        for n1, c1 in count1.items():\n            for n2, c2 in count2.items():\n                n3 = (n1 ** 2) / n2\n                c3 = count2[n3]\n                if n2 == n3:\n                    triplets += c1 * c3 * (c3 - 1) / 2\n                else:\n                    triplets += (c1 * c2 * c3) / 2\n                    \n                # print(n1, n2, n3, triplets)\n                \n        return int(triplets)", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        def find(target, arr):\n            seen = Counter()\n            count = 0\n            for a in arr:\n                if target % a == 0:\n                    count += seen[target // a]\n                seen[a] += 1\n            return count\n            \n        @lru_cache(None)\n        def twoProduct1(target):\n            return find(target, nums1)\n\n        @lru_cache(None)\n        def twoProduct2(target):\n            return find(target, nums2)\n\n        return sum(twoProduct2(a*a) for a in nums1) + sum(twoProduct1(a*a) for a in nums2)", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        sq1 = Counter([n**2 for n in nums1])\n        sq2 = Counter([n**2 for n in nums2])\n        triplets = self.count_triplets(sq1, nums2)\n        triplets += self.count_triplets(sq2, nums1)\n        return triplets\n    \n    def count_triplets(self, sqs: List[int], nums: List[int]) -> int:\n        pairs = defaultdict(int)\n        triplets = 0\n        for n in nums:\n            if n in pairs:\n                triplets += pairs[n]\n            for k in sqs:\n                pairs[k/n] += sqs[k]\n        return triplets", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n      num1Map = defaultdict(int ) \n      for i,n in enumerate(nums1):\n        num1Map[n]+=1\n        \n      num2Map = defaultdict(int) \n      for i,n in enumerate(nums2):\n        num2Map[n]+=1\n        \n      def count(nums,numsMap):\n        out = 0\n        same = 0\n        for n1 in nums:\n          sq = n1 * n1\n          for n2,v in list(numsMap.items()):\n            if sq/n2 == sq//n2:\n              k =numsMap.get(sq//n2)\n              if sq//n2 == n2 :\n                if k > 1:\n                  same+= math.factorial(k)// (math.factorial(k-2) * math.factorial(2)  )\n              elif k:\n                out+= k * v\n        return same +out//2 \n      \n      out=0   \n      out +=count(nums1,num2Map)     \n      out +=count(nums2,num1Map)     \n      return out \n      \n      \n", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        \n        def countTriplets(nums1, nums2):\n            num2idx = defaultdict(list)\n            for i, n in enumerate(nums2):\n                num2idx[n].append(i)\n            \n            #print(num2idx)\n            res = 0\n            ct1 = Counter(nums1)\n            for n in ct1:\n                nsq = n**2\n                for i, n1 in enumerate(nums2):\n                    n2, mod2 = divmod(nsq, n1)\n                    #print(n1, n2, n, mod2)\n                    if mod2 == 0 and n2 in num2idx:\n                        #print(n1, n2, nsq, len([x for x in num2idx[n2] if x > i]))\n                        res += len([x for x in num2idx[n2] if x > i]) * ct1[n]\n            return res\n        \n        return countTriplets(nums1, nums2) +  countTriplets(nums2, nums1)\n                        \n        \n", "from collections import Counter\n\nclass Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        c1 = Counter(nums1)\n        c2 = Counter(nums2)\n        ans = 0\n        for x in nums1:\n            sqr = x*x\n            for y in c2.keys():\n                if sqr%y == 0:\n                    z = sqr//y\n                    if y == z:\n                        ans += c2[y] * (c2[y]-1)\n                    else:\n                        ans+= c2[y] * c2.get(z, 0)\n        for x in nums2:\n            sqr = x*x\n            for y in c1.keys():\n                if sqr%y == 0:\n                    z = sqr//y\n                    if y == z:\n                        ans += c1[y] * (c1[y]-1)\n                    else:\n                        ans+= c1[y] * c1.get(z, 0)\n        return ans//2", "from collections import Counter\n\nclass Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        count1 = Counter(nums1)\n        count2 = Counter(nums2)\n        return self.count_triplets(count1, count2) + self.count_triplets(count2, count1)\n        \n    def count_triplets(self, count1, count2):\n        triplets = 0\n        for n1, c1 in count1.items():\n            for n2, c2 in count2.items():\n                n3 = (n1 ** 2) / n2\n                c3 = count2[n3]\n                if n2 == n3:\n                    triplets += c1 * c3 * (c3 - 1) / 2\n                else:\n                    triplets += (c1 * c2 * c3) / 2\n                                    \n        return int(triplets)", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        nums1.sort()\n        nums2.sort()\n        count=self.counttrip(nums1,nums2)\n        \n        count+=self.counttrip(nums2,nums1)\n        return int(count)\n        \n    def counttrip(self,nums1,nums2):\n        seen=defaultdict(int)\n        count=0\n        for i in nums1:\n            p=i*i\n            if(p in seen):\n                count+=seen[p]\n                continue\n            \n            localcount=0\n            left=0\n            right = len(nums2)-1\n            \n            while(left<right):\n                if(nums2[left]>p):\n                    break\n                if(nums2[left]*nums2[right]==p):\n                    if(nums2[left]==nums2[right]):\n                        n=right-left+1\n                        localcount+=n*(n-1)/2\n                        break\n                    localcount+=1\n                    nleft=left+1\n                    while(nums2[nleft]==nums2[nleft-1] and nleft<right):\n                        nleft+=1\n                        localcount+=1\n                    right-=1\n                    left=0\n                elif(nums2[left]*nums2[right]>p):\n                    \n                    right-=1\n                elif(nums2[left]*nums2[right]<p):\n                    left+=1\n            count+=localcount       \n            seen[p]=localcount\n                    \n        return count\n", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        def cal(arr1, arr2):\n            countMap = {}\n            m = defaultdict(list)\n            count = 0\n            for i in range(len(arr2)): m[arr2[i]].append(i)\n            for e in arr1:\n                if e in countMap: \n                    count += countMap[e]\n                    continue\n                c = 0\n                for i in range(len(arr2)):\n                    if not e*e%arr2[i]:\n                        d = e*e//arr2[i]\n                        c += sum([0]+[1 for idx in m[d] if idx != i])\n                countMap[e] = c\n                count += c\n                        \n            return count//2\n        return cal(nums1, nums2) + cal(nums2, nums1)", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        def count(arr1, arr2):\n            res = 0\n            dic = collections.Counter(arr1)\n            for a in dic:\n                counter = collections.Counter()\n                for b in arr2:\n                    c = a * a / b\n                    if c in counter:\n                        res += counter[c] * dic[a]\n                    counter[b] += 1\n            return res\n        res = count(nums1, nums2)\n        res += count(nums2, nums1)\n        return res", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        lookup1 = dict()\n        lookup2 = dict()\n        for num1 in nums1:\n            lookup1[num1] = lookup1.get(num1, 0) + 1\n        for num2 in nums2:\n            lookup2[num2] = lookup2.get(num2, 0) + 1\n        mem = dict()\n        \n        \n        ans = 0\n        ans1 = 0\n        ans2 = 0\n        for idx1 in range(len(nums1)):\n            num1 = nums1[idx1]\n            if num1 in mem:\n                ans1 += mem[num1][0]\n                ans2 += mem[num1][1]\n                continue\n            lookup1[num1] -= 1\n            ans1_tmp = 0\n            ans2_tmp = 0\n            for idx2 in range(len(nums2)):\n                num2 = nums2[idx2]\n                lookup2[num2] -= 1\n                if lookup2.get( (num1**2 / num2), 0 ) > 0:\n                    #print(num1, num2, lookup2)\n                    ans1_tmp += lookup2[(num1**2 / num2)]\n                lookup2[num2] += 1\n                \n                \n                if lookup1.get( (num2**2 / num1), 0 ) > 0:\n                    ans2_tmp += lookup1[(num2**2 / num1)]\n            mem[nums1[idx1]] = (ans1_tmp, ans2_tmp)\n            ans1 += ans1_tmp\n            ans2 += ans2_tmp\n                    \n            lookup1[num1] += 1\n            \n        return ans1 // 2 + ans2 // 2", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        def find(target, arr):\n            seen = Counter()\n            count = 0\n            for a in arr:\n                if target % a == 0:\n                    count += seen[target // a]\n                seen[a] += 1\n            return count\n            \n        @lru_cache(None)\n        def twoProduct1(target):\n            return find(target, nums1)\n\n        @lru_cache(None)\n        def twoProduct2(target):\n            return find(target, nums2)\n\n        return sum(twoProduct2(a*a) for a in nums1) + sum(twoProduct1(a*a) for a in nums2)\n    \n    def numTriplets2(self, nums1: List[int], nums2: List[int]) -> int:\n        d1 = collections.defaultdict(int)\n        d2 = collections.defaultdict(int)\n        for i in nums1:\n            d1[i * i] += 1\n        for i in nums2:\n            d2[i * i] += 1\n        res = 0\n        for i in range(len(nums1) - 1):\n            for j in range(i + 1, len(nums1)):\n                p = nums1[i] * nums1[j]\n                if p in d2:\n                    res += d2[p]\n        for i in range(len(nums2) - 1):\n            for j in range(i + 1, len(nums2)):\n                p = nums2[i] * nums2[j]\n                if p in d1:\n                    res += d1[p]\n        return res\n    \n    def numTriplets3(self, A1: List[int], A2: List[int]) -> int:\n        A1.sort()\n        A2.sort()\n        C1,C2=collections.Counter(A1),collections.Counter(A2)\n        # print(A1,A2,C1,C2)\n        res=0\n        N1,N2=len(A1),len(A2)\n        seen=set()\n        for i,x in enumerate(A1):\n            for j,y1 in enumerate(A2):\n                if x*x<y1:break\n                # if y1 in seen:continue\n                y2=x*x//y1\n                if x*x%y1==0 and y2 in C2:\n                    c=C2[y2]\n                    # print(x,y1,c)\n                    if x==y1:res+=comb(c,2)# if c>1 else 0\n                    else:res+=c*C2[y1]\n                    seen|={y1,y2}\n                    break\n        # print(res)\n        seen=set()\n        for x in A2:\n            for y1 in A1:\n                if x*x<y1:break\n                # if y1 in seen:continue\n                y2=x*x//y1\n                if x*x%y1==0 and y2 in C1:\n                    c=C1[y2]\n                    # print('-',x,y1,c)\n                    if x==y1:res+=comb(c,2)# if c>1 else 0\n                    else:res+=c*C1[y1]\n                    seen|={y1,y2}\n                    break\n        return res", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        \n        prod_nums1 = collections.Counter([v1 * v2 for v1,v2 in itertools.combinations(nums1,2)])\n        prod_nums2 = collections.Counter([v1 * v2 for v1,v2 in itertools.combinations(nums2,2)])\n        \n        return sum([prod_nums2[n1**2] for n1 in nums1]) + sum([prod_nums1[n2**2] for n2 in nums2])\n        \n        \n        \n", "from collections import defaultdict\n\nclass Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        count = 0\n        nums1.sort()\n        nums2.sort()\n        \n        def search(array1, array2):\n            result = 0\n            hashtable = defaultdict(int)\n            \n            for i in array2:\n                    hashtable[i] += 1\n            \n            for a in array1:\n                squared = a ** 2\n                \n                met = []\n                for i in list(hashtable.keys()):\n                    if i not in met:\n                        if squared % i == 0:\n                            complement = squared // i\n                            if complement != i:\n                                result += hashtable[i] * hashtable[complement]\n                                met.append(complement)\n                                # print(hashtable[i] * hashtable[complement], i, complement)\n                                # hashtable[i] = 0\n                                # hashtable[complement] = 0\n                            else:\n                                met.append(i)\n                                result += sum([j for j in range(hashtable[i])])\n                                # print(hashtable[i], i, complement)\n                                # hashtable[i] = 0\n                        \n            \n            return result\n                        \n        count += search(nums1, nums2)\n        count += search(nums2, nums1)\n        \n        return count\n                            \n                        \n                        \n", "from collections import Counter\nfrom itertools import combinations\n\nclass Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        c1 = Counter([x*y for x, y in combinations(nums1, 2)])\n        c2 = Counter([x*y for x, y in combinations(nums2, 2)])\n        return sum([c1[x*x] for x in nums2]) + sum([c2[x*x] for x in nums1])\n", "from collections import Counter\nclass Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        n1_cnts, n2_cnts = Counter(nums1), Counter(nums2)\n        \n        good = 0\n        n2_cnts_copy = n2_cnts.copy()\n        for n2j in nums2:\n            n2_cnts_copy[n2j] -= 1\n            for n1, cnt in list(n1_cnts.items()):\n                good += cnt*n2_cnts_copy[n1**2/n2j]\n                \n        for n1j in nums1:\n            n1_cnts[n1j] -= 1\n            for n2, cnt in list(n2_cnts.items()):\n                good += cnt*n1_cnts[n2**2/n1j]\n                \n        return good\n        \n", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        def helper(nums, lookup):\n            # print('nums = {0}, lookup = {1}'.format(nums, lookup))\n            res = 0\n            for maxNum in nums:\n                prod = maxNum * maxNum\n                # print('maxNum = {0}, prod = {1}'.format(maxNum, prod))\n                for n in lookup.keys():\n                    if n <= maxNum and not prod % n:\n                        m = prod // n\n                        # print('m = {0}, n = {1}'.format(m, n))\n                        \n                        res += (lookup[n] * (lookup[n] - 1)) // 2 if m == n else lookup[m] * lookup[n]\n            return res\n        return helper(nums1, collections.Counter(nums2)) + helper(nums2, collections.Counter(nums1))\n    \n        \n    # slown\n    def numTriplets1(self, nums1: List[int], nums2: List[int]) -> int:\n        lookup1, lookup2 = collections.defaultdict(int), collections.defaultdict(int)\n        for n in nums1:\n            lookup1[n] += 1\n        for n in nums2:\n            lookup2[n] += 1\n        res = 0\n        for k in [0, 1]:\n            nums = nums1 if not k else nums2\n            lookup = lookup1 if k else lookup2\n            for i in range(len(nums)):\n                for j in range(i + 1, len(nums)):\n                    prod = nums[i] * nums[j]\n                    k = int(math.sqrt(prod))\n                    if k * k == prod and k in lookup:\n                        res += lookup[k]\n        return res", "from collections import defaultdict\nclass Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        n1_cnts, n2_cnts = defaultdict(int), defaultdict(int)\n        \n        for n1 in nums1:\n            n1_cnts[n1] += 1\n        \n        for n2 in nums2:\n            n2_cnts[n2] += 1\n        \n        good = 0\n        n2_cnts_copy = n2_cnts.copy()\n        for n2j in nums2:\n            n2_cnts_copy[n2j] -= 1\n            for n1, cnt in list(n1_cnts.items()):\n                good += cnt*n2_cnts_copy[n1**2/n2j]\n                \n        for n1j in nums1:\n            n1_cnts[n1j] -= 1\n            for n2, cnt in list(n2_cnts.items()):\n                good += cnt*n1_cnts[n2**2/n1j]\n                \n        return good\n        \n", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        d1 = collections.defaultdict(int)\n        d2 = collections.defaultdict(int)\n        for i in nums1:\n            d1[i * i] += 1\n        for i in nums2:\n            d2[i * i] += 1\n        res = 0\n        for i in range(len(nums1) - 1):\n            for j in range(i + 1, len(nums1)):\n                p = nums1[i] * nums1[j]\n                if p in d2:\n                    res += d2[p]\n        for i in range(len(nums2) - 1):\n            for j in range(i + 1, len(nums2)):\n                p = nums2[i] * nums2[j]\n                if p in d1:\n                    res += d1[p]\n        return res\n#         pow_nums1 = [ x**2 for x in nums1 ]\n#         pow_nums2 = [ x**2 for x in nums2 ]\n#         count = 0\n\n#         # type 1\n#         for num_index in range(len(nums2) - 1): # exclude the last one\n#             current = nums2[num_index]\n#             rests = nums2[num_index + 1:]\n#             for rest in rests:\n#                 product = current * rest\n#                 count += pow_nums1.count(product)\n#         # type 2\n#         for num_index in range(len(nums1) - 1): # exclude the last one\n#             current = nums1[num_index]\n#             rests = nums1[num_index + 1:]\n#             for rest in rests:\n#                 product = current * rest\n#                 count += pow_nums2.count(product)\n                    \n\n#         return count\n    \n", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        \n        m1=collections.defaultdict(lambda: 0)\n        m2=collections.defaultdict(lambda: 0)\n        \n        for i in range(len(nums1)):\n            m1[nums1[i]**2]+=1\n        for i in range(len(nums2)):\n            m2[nums2[i]**2]+=1\n        \n#         print(m1)\n#         print(m2)\n        ans=0\n        for i in range(len(nums1)):\n            for j in range(i+1, len(nums1)):\n                # if(i==j):\n                #     continue\n                ans+=m2[nums1[i]*nums1[j]]\n                \n        for i in range(len(nums2)):\n            for j in range(i+1, len(nums2)):\n                # if(i==j):\n                #     continue\n                ans+=m1[nums2[i]*nums2[j]]\n        \n        return ans", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        def prod_finder(num_list1, num_list2):\n            prod_map = {}\n            for n in num_list1:\n                prod_map[n*n] = 0\n            for n in num_list1:\n                prod_map[n*n] += 1\n                \n            count = 0\n            \n            for i in range(0, len(num_list2)):\n                for j in range(i+1, len(num_list2)):\n                    p = num_list2[i] * num_list2[j]\n                    if p in prod_map:\n                        count += prod_map[p]\n            return count\n        return prod_finder(nums1, nums2) + prod_finder(nums2, nums1)", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        # TC : O(n**2), where n = max(#nums1, #nums2)\n        nums1_sq_dct = collections.defaultdict(int)\n        nums2_sq_dct = collections.defaultdict(int)\n        \n        res = 0\n        \n        for x in nums1:\n            nums1_sq_dct[x**2] += 1\n        for y in nums2:\n            nums2_sq_dct[y**2] += 1\n            \n        for i in range(len(nums1)):\n            for j in range(i+1, len(nums1)) :\n                res += nums2_sq_dct[nums1[i]*nums1[j]]\n        \n        for i in range(len(nums2)):\n            for j in range(i+1, len(nums2)) :\n                res += nums1_sq_dct[nums2[i]*nums2[j]]\n        \n        return res", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        d1,d2 = {}, {}\n        for v in nums1:\n            d1[v*v] = d1.get(v*v,0) + 1\n        for v in nums2:\n            d2[v*v] = d2.get(v*v,0) + 1\n        res = 0\n        for i,v in enumerate(nums1):\n            for j in range(i+1, len(nums1)):\n                res += d2.get(v*nums1[j], 0)\n        for i,v in enumerate(nums2):\n            for j in range(i+1, len(nums2)):\n                res += d1.get(v*nums2[j], 0)\n        return res", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        seta={}\n        setb={}\n        for i in nums1:\n            if i*i in seta:\n                seta[i*i]+=1\n            else:\n                seta[i*i]=1\n        for i in nums2:\n            if i*i in setb:\n                setb[i*i]+=1\n            else:\n                setb[i*i]=1\n        ans=0\n        for i in range(len(nums1)-1):\n            for j in range(i+1,len(nums1)):\n                if nums1[i]*nums1[j] in setb:\n                    ans+=setb[nums1[i]*nums1[j]]\n        for i in range(len(nums2)-1):\n            for j in range(i+1,len(nums2)):\n                if nums2[i]*nums2[j] in seta:\n                    ans+=seta[nums2[i]*nums2[j]]\n        return ans", "class Solution:\n    # https://leetcode.com/submissions/detail/404293310/\n    # fast\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        def helper(nums, lookup):\n            res = 0\n            for maxNum in nums:\n                prod = maxNum * maxNum\n                for n in lookup.keys():\n                    if n <= maxNum and not prod % n:\n                        m = prod // n\n                        # m ==n, combination.\n                        res += (lookup[n] * (lookup[n] - 1)) // 2 if m == n else lookup[m] * lookup[n]\n            return res\n        return helper(nums1, collections.Counter(nums2)) + helper(nums2, collections.Counter(nums1))\n    \n        \n    # slow\n    def numTriplets1(self, nums1: List[int], nums2: List[int]) -> int:\n        lookup1, lookup2 = collections.defaultdict(int), collections.defaultdict(int)\n        lookup1, lookup2 = collections.Counter(nums1), collections.Counter(nums2)\n        res = 0\n        for k in [0, 1]:\n            nums = nums1 if not k else nums2\n            lookup = lookup1 if k else lookup2\n            for i in range(len(nums)):\n                for j in range(i + 1, len(nums)):\n                    prod = nums[i] * nums[j]\n                    k = int(math.sqrt(prod))\n                    if k * k == prod and k in lookup:\n                        res += lookup[k]\n        return res", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        d1 = defaultdict(int)\n        d2 = defaultdict(int)\n        ans = 0\n        \n        for i in nums1:\n            d1[i*i] += 1\n        for i in nums2:\n            d2[i*i] += 1\n        \n        for i in range(len(nums1)):\n            for j in range(i+1,len(nums1)):\n                ans += d2[nums1[i]*nums1[j]]\n        \n        for i in range(len(nums2)):\n            for j in range(i+1,len(nums2)):\n                ans += d1[nums2[i]*nums2[j]]\n        \n        \n        return ans", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        \n        count = 0\n        d1 = {}\n        d2 = {}\n        \n        # comparing nums1^2 with nums2 product\n        for i in nums1:\n            if i**2 not in d1:\n                d1[i**2] = 1\n            else:\n                d1[i**2]+= 1\n        \n        for j in range(len(nums2)):\n            for k in range(j+1,len(nums2)):\n                prod = nums2[j]*nums2[k]\n                if prod in d1:\n                    count+=d1[prod]\n                    \n        # comparing nums2^2 with nums1 product\n        for i in nums2:\n            if i**2 not in d2:\n                d2[i**2] = 1\n            else:\n                d2[i**2]+= 1\n        \n        for j in range(len(nums1)):\n            for k in range(j+1,len(nums1)):\n                prod = nums1[j]*nums1[k]\n                if prod in d2:\n                    count+=d2[prod]\n    \n        \n        return count", "from collections import defaultdict\nclass Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        count = 0\n        d1 = defaultdict(int)\n        d2 = defaultdict(int)\n        \n        for n in nums1:\n            d1[n*n] += 1\n        \n        for n in nums2:\n            d2[n*n] += 1\n            \n        count = 0\n        for i in range(0, len(nums1)-1):\n            for j in range(i+1, len(nums1)):\n                count += d2[nums1[i]*nums1[j]]\n                    \n        for i in range(0, len(nums2)-1):\n            for j in range(i+1, len(nums2)):\n                count += d1[nums2[i]*nums2[j]]\n                    \n        return count\n        \n        \n        \n", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        sq1 = collections.Counter([nums1[i] * nums1[j] for i in range(len(nums1)) for j in range(i)])\n        sq2 = collections.Counter([nums2[i] * nums2[j] for i in range(len(nums2)) for j in range(i)])\n        return sum(sq1[n**2] for n in nums2) + sum(sq2[n**2] for n in nums1)", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        sqr_nums1 = defaultdict(int)\n        sqr_nums2 = defaultdict(int)\n        res = 0\n        for x in nums1:\n            sqr_nums1[x*x] += 1\n        for x in nums2:\n            sqr_nums2[x*x] += 1\n        for i in range(len(nums1)-1):\n            for j in range(i+1,len(nums1)):\n                res += sqr_nums2[nums1[i]*nums1[j]]\n        for i in range(len(nums2)-1):\n            for j in range(i+1,len(nums2)):\n                res += sqr_nums1[nums2[i]*nums2[j]]\n        return res\n                \n", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        \n        hashmap1= {}\n        hashmap2= {}\n        count =0\n        \n        for i in range(len(nums1)):\n            if nums1[i]*nums1[i] not in hashmap1:\n                hashmap1[nums1[i]*nums1[i]] =1\n            else:\n                hashmap1[nums1[i]*nums1[i]] +=1\n                \n           \n        for j in range(len(nums2)):\n            if nums2[j]*nums2[j] not in hashmap2:\n                hashmap2[nums2[j]*nums2[j]]=1\n            else:\n                hashmap2[nums2[j]*nums2[j]]+=1\n                \n        for i in range(len(nums1)):\n            for j in range(i+1,len(nums1)):\n                if nums1[i]*nums1[j] in hashmap2:\n                    count+=hashmap2[nums1[i]*nums1[j]]\n                    \n                \n                \n        for i in range(len(nums2)):\n            for j in range(i+1,len(nums2)):\n                if nums2[i]*nums2[j] in hashmap1:\n                    count+=hashmap1[nums2[i]*nums2[j]]\n        \n        \n        return count\n", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        '''\n        primes = []\n        available = [True for _ in range(100000)]\n        available[0] = False\n        available[1] = False\n        k = 0\n        \n        while True:\n            while k < len(available) and available[k] == False:\n                k += 1\n            if k == len(available):\n                break\n            primes.append(k)\n            n = 1\n            while n * k < len(available):\n                available[n * k] = 0\n                n += 1\n            \n        def factorize(x):\n            result = collections.Counter()\n            if x == 1:\n                return result\n            for p in primes:\n                if x % p == 0:\n                    result[p] += 1\n                    return result + factorize(x // p)\n                \n        print(factorize(7 * 13 * 5))\n        \n        fac1 = [factorize(n) for n in nums1]\n        fac2 = [factorize(n) for n in nums2]\n        '''\n        \n        prod1 = collections.defaultdict(int)\n        for i in range(len(nums1)):\n            for j in range(i + 1, len(nums1)):\n                prod1[nums1[i] * nums1[j]] += 1\n        \n        prod2 = collections.defaultdict(int)\n        for i in range(len(nums2)):\n            for j in range(i + 1, len(nums2)):\n                prod2[nums2[i] * nums2[j]] += 1\n                \n        count = 0\n        for n in nums1:\n            count += prod2[n * n]\n                \n        for n in nums2:\n            count += prod1[n * n]\n                \n        return count\n                \n        \n        \n        \n                \n                \n", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        dic1, dic2 = collections.defaultdict(int), collections.defaultdict(int)\n        \n        for num in nums1:\n            dic1[num**2] += 1\n        for num in nums2:\n            dic2[num**2] += 1\n            \n        res = 0\n        for i in range(len(nums1)):\n            for j in range(i+1, len(nums1)):\n                res += dic2[nums1[i] * nums1[j]]\n        \n        for i in range(len(nums2)):\n            for j in range(i+1, len(nums2)):\n                res += dic1[nums2[i] * nums2[j]]\n        \n        return res", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        d1 = collections.defaultdict(int)\n        d2 = collections.defaultdict(int)\n        for i in nums1:\n            d1[i * i] += 1\n        for i in nums2:\n            d2[i * i] += 1\n        \n        ans = 0\n        for i in range(len(nums1)-1):\n            for j in range(i+1,len(nums1)):\n                x = nums1[i] * nums1[j]\n                ans += d2[x]\n        for i in range(len(nums2)-1):\n            for j in range(i+1,len(nums2)):\n                x = nums2[i] * nums2[j]\n                ans += d1[x]\n        return ans \n                               \n                    \n        \n            \n", "import collections\nclass Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        if len(nums1)<=1 and len(nums2)<=1:\n            return 0\n        numTri = 0\n        nums1Sq = [num**2 for num in nums1]\n        nums2Sq = [num**2 for num in nums2]\n        nums1Mul = collections.Counter([nums1[i]*nums1[j] for i in range(len(nums1)) for j in range(i+1,len(nums1))])\n        nums2Mul = collections.Counter([nums2[i]*nums2[j] for i in range(len(nums2)) for j in range(i+1,len(nums2))])\n        for num in nums1Sq:\n            if num in nums2Mul:\n                numTri += nums2Mul[num] \n        for num in nums2Sq:\n            if num in nums1Mul:\n                numTri += nums1Mul[num] \n        return numTri", "from collections import Counter\n\n\nclass Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        counter1 = Counter(nums1)\n        counter2 = Counter(nums2)\n\n        ret = 0\n        for k1, v1 in counter1.items():\n            for k2, v2 in counter2.items():\n                if k1*k1/k2 in counter2:\n                    if k2 == k1:\n                        ret += v1*v2*(v2-1)\n                    else:\n                        ret += v1*v2*counter2[k1*k1/k2]\n\n        for k2, v2 in counter2.items():\n            for k1, v1 in counter1.items():\n                if k2*k2/k1 in counter1:\n                    if k2 == k1:\n                        ret += v2*v1*(v1-1)\n                    else:\n                        ret += v1*v2*counter1[k2*k2/k1]\n                        \n        return int(ret/2)", "from collections import defaultdict\n\nclass Solution:\n  def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n    n1 = len(nums1)\n    n2 = len(nums2)\n    n1_squares = defaultdict(int)\n    n2_squares = defaultdict(int)\n    for num1 in nums1:\n      n1_squares[num1*num1] += 1\n      pass\n    for num2 in nums2:\n      n2_squares[num2*num2] += 1\n      pass\n    triplets = 0\n    for i in range(n1-1):\n      for j in range(i+1,n1):\n        sq = nums1[i]*nums1[j]\n        triplets += n2_squares[sq]\n    for i in range(n2-1):\n      for j in range(i+1,n2):\n        sq = nums2[i]*nums2[j]\n        triplets += n1_squares[sq]\n    return triplets", "from collections import defaultdict\n\n\nclass Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        cnt1, cnt2 = defaultdict(int), defaultdict(int)\n        for i in range(len(nums1)):\n            for j in range(i + 1, len(nums1)):\n                cnt1[nums1[i] * nums1[j]] += 1\n        for i in range(len(nums2)):\n            for j in range(i + 1, len(nums2)):\n                cnt2[nums2[i] * nums2[j]] += 1\n        ans = 0\n        for i in nums1: ans += cnt2[i * i]\n        for i in nums2: ans += cnt1[i * i]\n        return ans", "class Solution:\n    \n    def work(self, nums1, nums2):\n        res =0\n        dic ={}\n        for num in nums1:\n            if num*num in dic:\n                dic[num*num] +=1\n            else:\n                dic[num*num] =1\n        \n        n =len(nums2)\n        for i in range(n):\n            for j in range(i+1, n):\n                if nums2[i] * nums2[j] in dic:\n                    res += dic[nums2[i] * nums2[j]]\n        return res\n    \n        \n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        return self.work(nums1, nums2) + self.work(nums2, nums1);\n", "class Solution:\n    def numTriplets(self, a1: List[int], a2: List[int]) -> int:\n        m1 = Counter([x**2 for x in a1])\n        m2 = Counter([x**2 for x in a2])\n        ans = 0\n        n1, n2 = len(a1), len(a2)\n        for i in range(n1-1):\n            for j in range(i+1, n1):\n                k = a1[i] * a1[j]\n                if k in m2: ans += m2[k]\n        for i in range(n2-1):\n            for j in range(i+1, n2):\n                k = a2[i] * a2[j]\n                if k in m1: ans += m1[k]\n        return ans", "from typing import List\nfrom collections import Counter\n\nclass Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        A = Counter([nums1[i]**2 for i in range(len(nums1))])\n        B = Counter([nums2[i]**2 for i in range(len(nums2))])\n        cnt = 0\n        for j in range(len(nums2)):\n            for k in range(j + 1, len(nums2)):\n                cnt += A.get(nums2[j]*nums2[k], 0)\n        for j in range(len(nums1)):\n            for k in range(j + 1, len(nums1)):\n                cnt += B.get(nums1[j]*nums1[k], 0)\n        return cnt\n", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        \n        d = {}\n        ct = 0\n        M = len(nums1)\n        N = len(nums2)\n        \n        for i in range(M):\n            if(nums1[i]*nums1[i] in d):\n                d[nums1[i]*nums1[i]] += 1\n            else:\n                d[nums1[i]*nums1[i]] = 1\n        \n        for i in range(N):\n            for j in range(i+1,N):\n                if(nums2[i]*nums2[j] in d):\n                    ct += d[nums2[i]*nums2[j]]\n        d = {}\n        for i in range(N):\n            if(nums2[i]*nums2[i] in d):\n                d[nums2[i]*nums2[i]] += 1\n            else:\n                d[nums2[i]*nums2[i]] = 1\n        \n        for i in range(M):\n            for j in range(i+1,M):\n                if(nums1[i]*nums1[j] in d):\n                    ct += d[nums1[i]*nums1[j]]\n        return ct", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        res = 0\n        d1 = collections.defaultdict(int)\n        d2 = collections.defaultdict(int)\n        \n        for v in nums1:\n            d1[v * v] += 1\n        for v in nums2:\n            d2[v * v] += 1\n            \n        for i in range(len(nums2) - 1):\n            for j in range(i + 1, len(nums2)):\n                if  nums2[i] * nums2[j] in d1:\n                    res += d1[nums2[i] * nums2[j]]\n\n        for i in range(len(nums1) - 1):\n            for j in range(i + 1, len(nums1)):\n                if nums1[i] * nums1[j] in d2:\n                    res += d2[nums1[i] * nums1[j]]\n        return res\n", "from collections import defaultdict\nclass Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        def get_sq_cnt(nums):\n            cnt = defaultdict(int)\n            n = len(nums)\n            for i in range(n):\n                for j in range(i+1, n):\n                    cnt[nums[i]*nums[j]] +=1\n            return cnt\n        \n        cnt1, cnt2 = get_sq_cnt(nums1), get_sq_cnt(nums2)\n        res = 0\n        for num1 in nums1:\n            res += cnt2[num1*num1]\n        for num2 in nums2:\n            res += cnt1[num2*num2]\n        \n        return res\n", "class Solution:\n    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n        dict1 = Counter([x**2 for x in nums1])\n        dict2 = Counter([x**2 for x in nums2])\n        \n        count=0\n        for i in range(len(nums2)-1):\n            for j in range(i+1,len(nums2)):\n                count += dict1[nums2[i]*nums2[j]]\n        for i in range(len(nums1)-1):\n            for j in range(i+1,len(nums1)):\n                count += dict2[nums1[i]*nums1[j]]\n        \n        return count", "class Solution:\n    def numTriplets(self, f: List[int], s: List[int]) -> int:\n        from collections import defaultdict\n        \n        \n        def helper(nums1, nums2):\n            d = defaultdict(int)\n            ans = 0    \n\n            for i in range(len(nums1)):\n                for j in range(i+1, len(nums1)):\n                    d[nums1[i]* nums1[j]]+= 1\n\n            for num in nums2:\n                ans += d[num**2]\n            return ans\n                \n        return helper(f,s )+ helper(s,f)"]