C++ O(MN) with explanation
See my latest update in repo LeetCode\n\n## Solution 1.\n\nType 1 and Type 2 are symmetrical so we can define a function count(A, B) which returns the count of 
See my latest update in repo [LeetCode](https://github.com/lzl124631x/LeetCode)\n\n## Solution 1.\n\nType 1 and Type 2 are symmetrical so we can define a function `count(A, B)` which returns the count of the Type 1 triples. The answer is `count(A, B) + count(B, A)`.\n\nFor `count(A, B)`, we can use a `unordered_map<int, int> m` to store the frequency of the numbers in `B`. Then for each number `a` in `A`, the `target` value is `a * a`. We traverse the map `m` to count the triplets.\n\nFor each entry `(b, cnt)` in `m`:\n* If `target` is not divisible by `b` or `target / b` is not in `m`, there is no triplets, skip.\n* If `target / b == b`, we need to pick 2 out of `cnt` numbers so we can add `cnt * (cnt - 1)` triplets to the answer.\n* Otherwise, we can add `cnt * m[target / b]` triplets to the answer.\n\nSince we count the the pairs in `B` twice, we need to divide the answer by 2 before returning.\n\n```cpp\n// OJ: https://leetcode.com/problems/number-of-ways-where-square-of-number-is-equal-to-product-of-two-numbers/\n// Author: github.com/lzl124631x\n// Time: O(N^2)\n// Space: O(N)\nclass Solution {\n    int count(vector<int> &A, vector<int> &B) {\n        int ans = 0;\n        unordered_map<int, int> m;\n        for (int n : B) m[n]++;\n        for (int a : A) {\n            long target = (long)a * a;\n            for (auto &[b, cnt] : m) {\n                if (target % b || m.count(target / b) == 0) continue;\n                if (target / b == b) ans += cnt * (cnt - 1);\n                else ans += cnt * m[target / b];\n            }\n        }\n        return ans / 2;\n    }\npublic:\n    int numTriplets(vector<int>& A, vector<int>& B) {\n        return count(A, B) + count(B, A);\n    }\n};\n```
[]