[C++] Easy-understanding Top-Down DP Solution
The top-down idea is straightforward, just to find the total counts of each number, and sum up with sub-recursions. The sub-nodes can be found with simple facto
The top-down idea is straightforward, just to find the total counts of each number, and sum up with sub-recursions. The sub-nodes can be found with simple factor calculation, each number will be calculated only once.\n\n```\nclass Solution {\n    unordered_set<int> set;\n    unordered_map<int, long long> map;\n    long long mod = 1e9+7;\npublic:\n    long long findCount(vector<int> &arr, int num1) {\n        if(map.find(num1) != map.end()) return map[num1];\n\t\t// for each number, at least counts itself as leaf\n        long long cnt = 1;\n        for(int num2: arr) {\n\t\t\t// find sub-nodes\n            if(num2 <= num1 && num1 % num2 == 0 && set.count(num1 / num2)) {\n                cnt += findCount(arr, num2) * findCount(arr, num1 / num2);\n            }\n        }\n        return map[num1] = cnt;\n    }\n    int numFactoredBinaryTrees(vector<int>& arr) {\n        long long ans = 0;\n        set.insert(arr.begin(), arr.end());\n        for(int num: arr) {\n            ans += findCount(arr, num);\n            ans %= mod;\n        }\n        return ans;\n    }\n};\n```\n\nTime complexity: `O(n^2)`\nSpace complexity: `O(n)`
['Dynamic Programming', 'Memoization', 'C']