You are given an integer n representing the number of sheets.
You are also given an integer array limit of size m, where limit[i] is the maximum number of sheets that can be painted using color i.
You must paint all n sheets under the following conditions:
i cannot exceed limit[i].Return an integer denoting the number of distinct ways to paint all sheets. Since the answer may be large, return it modulo 109 + 7.
Note: Two ways differ if at least one sheet is painted with a different color.
Example 1:
Input: n = 4, limit = [3,1,2]
Output: 6
Explanation:
For each ordered pair(i, j), where color i is used for the first segment and color j for the second segment (i != j), a split of x and 4 - x is valid if 1 <= x <= limit[i] and 1 <= 4 - x <= limit[j].
Valid pairs and counts are:
(0, 1): x = 3(0, 2): x = 2, 3(1, 0): x = 1(2, 0): x = 1, 2Therefore, there are 6 valid ways in total.
Example 2:
Input: n = 3, limit = [1,2]
Output: 2
Explanation:
For each ordered pair (i, j), where color i is used for the first segment and color j for the second segment (i != j), a split of x and 3 - x is valid if 1 <= x <= limit[i] and 1 <= 3 - x <= limit[j].
Valid pairs and counts are:
(0, 1): x = 1(1, 0): x = 2Hence, there are 2 valid ways in total.
Example 3:
Input: n = 3, limit = [2,2]
Output: 4
Explanation:
For each ordered pair (i, j), where color i is used for the first segment and color j for the second segment (i != j), a split of x and 3 - x is valid if 1 <= x <= limit[i] and 1 <= 3 - x <= limit[j].
Valid pairs and counts are:
(0, 1): x = 1, 2(1, 0): x = 1, 2Therefore, there are 4 valid ways in total.
Constraints:
2 <= n <= 1092 <= m == limit.length <= 1051 <= limit[i] <= 109limit and use binary search (lower_bound) to compute num_ge(t), the number of colors with limit >= t.x, count ordered pairs (i, j) with i != j as num_ge(x) * num_ge(n - x) - num_ge(max(x, n - x)).num_ge(x) and num_ge(n - x) change only when x crosses 1, n - 1, L + 1, or n - L for some L in limit.x values, sort and deduplicate them, and treat the answer as constant between consecutive values.4 [3,1,2]
3 [1,2]
3 [2,2]