You are given a list of distinct integers nums and a target integer target. Determine how many different ordered sequences of numbers from nums (with repetition allowed) have a sum exactly equal to target. Different permutations of the same multiset of numbers count as separate solutions.
Return the total count of such sequences. The count is guaranteed to fit in a 32-bit integer.
Function Signature
def count_ordered_sequences(nums: list[int], target: int) -> int:
Rules
-
Each number in
nums
may be used any number of times in a sequence, including zero times.
-
Order matters:
(1, 2, 1)
and
(2, 1, 1)
are two different sequences.
-
Return
0
when no sequence reaches
target
.
Constraints
-
1 <= nums.length <= 150
-
1 <= nums[i] <= 750
-
1 <= target <= 750
-
All integers in
nums
are unique.
Examples
Input: nums = [1, 2], target = 4
Output: 5
The five ordered sequences are (1, 1, 1, 1), (1, 1, 2), (1, 2, 1), (2, 1, 1) and (2, 2). Sequences with the same numbers in a different order, such as (1, 2, 1) and (2, 1, 1), count separately.
Input: nums = [3, 5, 7], target = 10
Output: 3
The sequences are (3, 7), (7, 3) and (5, 5). No sequence of only 3s works, since 9 and 12 skip over 10.
Input: nums = [9], target = 3
Output: 0
The only available number is larger than the target, so no sequence sums to 3.