forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.cpp
31 lines (31 loc) · 1008 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
class Solution {
public:
int sumOfPowers(vector<int>& nums, int k) {
unordered_map<long long, int> f;
const int mod = 1e9 + 7;
int n = nums.size();
sort(nums.begin(), nums.end());
auto dfs = [&](auto&& dfs, int i, int j, int k, int mi) -> int {
if (i >= n) {
return k == 0 ? mi : 0;
}
if (n - i < k) {
return 0;
}
long long key = (1LL * mi) << 18 | (i << 12) | (j << 6) | k;
if (f.contains(key)) {
return f[key];
}
long long ans = dfs(dfs, i + 1, j, k, mi);
if (j == n) {
ans += dfs(dfs, i + 1, i, k - 1, mi);
} else {
ans += dfs(dfs, i + 1, i, k - 1, min(mi, nums[i] - nums[j]));
}
ans %= mod;
f[key] = ans;
return f[key];
};
return dfs(dfs, 0, n, k, INT_MAX);
}
};