-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path0823.cpp
More file actions
38 lines (35 loc) · 1.14 KB
/
Copy path0823.cpp
File metadata and controls
38 lines (35 loc) · 1.14 KB
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
33
34
35
36
37
38
class Solution {
static const int MOD = 1E9 + 7;
static int binary_search(const vector<int> &arr, const int end, const int target) {
int low = 0, high = end - 1;
while (low <= high) {
const int mid = low + (high - low) / 2;
if (arr[mid] == target) return mid;
if (arr[mid] > target)
high = mid - 1;
else
low = mid + 1;
}
return -1;
}
public:
int numFactoredBinaryTrees(vector<int> &arr) const {
static int mem[1000];
memset(mem, 0x00, sizeof(mem));
sort(begin(arr), end(arr));
long long res = 0;
for (int i = 0; i < arr.size(); i++) {
const int crnt = arr[i];
long long local = 1;
for (int j = 0; j < i; j++) {
if (crnt % arr[j] != 0) continue;
const int idx = binary_search(arr, i, crnt / arr[j]);
if (idx == -1) continue;
local = (local + (long long)mem[j] * mem[idx]) % MOD;
}
mem[i] = local;
res = (res + mem[i]) % MOD;
}
return res;
}
};