forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.cpp
32 lines (31 loc) · 855 Bytes
/
Solution.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
32
class Solution {
public:
const int mod = 1e9 + 7;
int numFactoredBinaryTrees(vector<int>& arr) {
sort(arr.begin(), arr.end());
unordered_map<int, int> idx;
int n = arr.size();
for (int i = 0; i < n; ++i) {
idx[arr[i]] = i;
}
vector<long> f(n, 1);
for (int i = 0; i < n; ++i) {
int a = arr[i];
for (int j = 0; j < i; ++j) {
int b = arr[j];
if (a % b == 0) {
int c = a / b;
if (idx.count(c)) {
int k = idx[c];
f[i] = (f[i] + 1l * f[j] * f[k]) % mod;
}
}
}
}
long ans = 0;
for (long v : f) {
ans = (ans + v) % mod;
}
return ans;
}
};