Problem Description
Given an array of non-negative integers and two integers l and r, count the number of sub-multisets (i.e. unordered selections where each number x can be taken 0 to its frequency times) such that the sum of the selected elements is within the inclusive range [l, r]. The answer should be returned modulo 10^9 + 7.
Key Insights
- The problem asks for counting multisets, which is equivalent to choosing 0 up to occurrence_count of each unique number.
- Due to potential duplicates, it’s more efficient to group numbers by their value.
- The sum of the array is at most 20000, allowing the use of a DP array with dimension equal to the maximum possible sum.
- Special handling for zeros is important since choosing zeros does not affect the sum but increases the number of possible multisets multiplicatively.
- The solution uses dynamic programming by updating the ways to form each sum when considering a new group of identical numbers.
Space and Time Complexity
Time Complexity: O(U * total_sum * max_frequency) in the worst-case scenario, where U is the number of distinct non-zero numbers.
Space Complexity: O(total_sum), where total_sum is the sum of the numbers (at most 20000).
Solution
We solve the problem using dynamic programming. First, we group the numbers by their value. We treat the number 0 as a special case: since adding a 0 does not change the sum, if there are f zeros, they contribute a multiplicative factor of (f+1) (as you can choose between 0 to f zeros). For each distinct non-zero number, we update a DP array that tracks the number of ways to obtain a certain sum. For each group with value x and frequency count, for each possible sum already achievable, we add ways by taking 0, 1, 2, ... up to count occurrences of that number (ensuring the sum does not exceed the total possible sum). Finally, we take the sum of DP values for sums in the target range [l, r] and multiply by the factor from zeros. All operations are performed modulo 10^9+7.