-
Notifications
You must be signed in to change notification settings - Fork 3
/
823.cpp
31 lines (31 loc) · 1.02 KB
/
823.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class Solution {
public:
int numFactoredBinaryTrees(vector<int>& arr) {
long long mod = 1e9 + 7;
unordered_map<int, long long> node2trees;
sort(arr.begin(), arr.end());
int n = arr.size();
for (int i = 0; i < n; ++i) {
int parent = arr[i];
node2trees[parent] = 1;
for (int j = 0; j < i; ++j) {
int childA = arr[j];
if (parent % childA != 0) continue;
int childB = parent / childA;
if (node2trees.find(childB) == node2trees.end()) continue;
long long cntA = node2trees[childA];
long long cntB = node2trees[childB];
long long curr = cntA * cntB;
curr %= mod;
node2trees[parent] += curr;
node2trees[parent] %= mod;
}
}
long long res = 0;
for (auto& element : node2trees) {
res += element.second;
res %= mod;
}
return res;
}
};