C++, 20ms, 12MB, max(O(n1+n2), O(d1*d2)) (d1, d2 are number of distinct numbers in nums1 and nums2)
Firstly we count how many times each number appeared in each vector.\n\nFor each distinct number n1 in one set, we try to find in the other set:\n  1. n2 == n1 
Firstly we count how many times each number appeared in each vector.\n\nFor each distinct number n1 in one set, we try to find in the other set:\n  1. `n2 == n1` and n2 appeared at least twice\n  2. `n2 < n1` and exists n2x that `n2*n2x == n1*n1` => `n2x == n1*n1/n2`\n\nFor `1`, if n1 appeared f1 times and n2 appeared f2 times, the total number of combinations is: `f1 * C(f2, 2)` = `f1 * (f2*(f2-1)/2)`\nFor `2`, if we can find both n2 and n2x, appeared f2 and f2x times respectively, the total number of combinations is: `f1 * (f2*f2x)` (note when n2 < n1, n2x > n1, we don\'t want to double count, so only check n2 < n1)\n\nWe count for type 1, switch them over and count for type 2, finally return the sum.\n\n----\n\nComplexity analysis:\n\n1. Counting number frequency is `O(n1 + n2)`\n2. Counting combinations, for each distinct number in one set, we check all numbers in the other set, then switch over and do it again: `O(freq1.size() * freq2.size() * 2)`, let d1=freq1.size(), d2=freq2.size() => `O(d1*d2)`\n\nEither `1` or `2` can take longer time, for example, if we have all same numbers like `[1,1,1,1,...,1,1]` and `[1,1,1,1,...,1,1]`, `1` takes much longer time than `2`. On the other hand if we have a lot of distinct numbers then `2` will be longer.\nSo the overall time complexity is `max(O(n1+n2), O(d1*d2))`.\n\nWe use 2 hash maps to store the frequencies, so space complexity is `O(d1+d2)`.\n\n```cpp\nint numTriplets(vector<int>& nums1, vector<int>& nums2) {\n    unordered_map<int64_t, int> freq1, freq2;\n    for (int n : nums1) freq1[n]++;\n    for (int n : nums2) freq2[n]++;\n\n    auto Count = [](unordered_map<int64_t, int> &freqX, unordered_map<int64_t, int> &freqY) {\n        int cnt = 0;\n        for (auto [n1, f1] : freqX) {\n            int c = 0;\n            int64_t n1sq = n1*n1;\n            for (auto [n2, f2] : freqY) {\n                if (n2 == n1) {\n                    if (f2 > 1) c += f2*(f2 - 1)/2;\n                } else if (n2 < n1 && n1sq % n2 == 0) {\n                    int n2x = n1sq/n2;\n                    if (freqY.count(n2x)) c += f2*freqY[n2x];\n                }\n            }\n            cnt += c*f1;\n        }\n        return cnt;\n    };\n\n    return Count(freq1, freq2) + Count(freq2, freq1);\n}\n```\n
['C']