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
40
41
42
43
44
45
46
47
|
#include <iostream>
#include <vector>
#include <cstring>
#include <unordered_map>
#include <queue>
const int M = 1'000'000'007;
class Solution {
public:
int countSubMultisets(std::vector<int>& nums, int l, int r) {
std::vector<int> dp(r + 1, 0);
std::unordered_map<int, int> occ;
for (int num : nums) {
++occ[num];
}
dp[0] = 1;
for (auto& pair : occ) {
if (!pair.first) {
continue;
}
int offset = (1 + pair.second) * pair.first;
std::queue<int> q;
for (int i = 0; i <= r; ++i) {
if (i + offset <= r) {
q.push(dp[i]);
}
if (i >= pair.first) {
dp[i] = (dp[i] + dp[i - pair.first]) % M;
}
if (i >= offset) {
dp[i] = (dp[i] + M - q.front()) % M;
q.pop();
}
}
}
long long res = 0;
for (int i = l; i <= r; ++i) {
res += dp[i];
}
return (res * (occ[0] + 1)) % M;
}
};
|