forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
count-of-sub-multisets-with-bounded-sum.cpp
39 lines (37 loc) · 1.27 KB
/
count-of-sub-multisets-with-bounded-sum.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
33
34
35
36
37
38
39
// Time: O(n + d * r), d = len(set(nums))
// Space: O(d + r)
// freq table, knapsack dp, sliding window, combinatorics
class Solution {
public:
int countSubMultisets(vector<int>& nums, int l, int r) {
static const int MOD = 1e9 + 7;
unordered_map<int, int> cnt;
for (const auto& x : nums) {
++cnt[x];
}
vector<int> dp(r + 1);
dp[0] = 1;
for (const auto& [x, c] : cnt) {
for (int i = r; i >= max(r - x + 1, 1); --i) {
int curr = 0;
for (int j = 0; j < c; ++j) {
if (i - x * j < 0) {
break;
}
curr = (curr + dp[i - x * j]) % MOD;
}
for (int j = i; j >= 1 ; j -= x) {
if (j - x * c >= 0) {
curr = (curr + dp[j - x * c]) % MOD;
}
curr = ((curr - dp[j]) % MOD + MOD) % MOD;
dp[j] = (dp[j] + curr) % MOD;
}
}
}
int64_t result = accumulate(cbegin(dp) + l, cbegin(dp) + (r + 1), 0, [&](const auto& accu, const auto& x) {
return (accu + x) % MOD;
});
return (result * (cnt[0] + 1)) % MOD;
}
};