All problems
0930HardArrayMathDynamic ProgrammingMemoizationNumber Theory

Selections of Throws Scoring Exactly k

Tracked in this browser only
Write code

Trains the technique from

LeetCode 3850Count Sequences to K

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

A run of dice throws is given as nums, each a number from 1 to 6.

A selection takes some of the throws, keeping their order, and its score is the product of the throws taken. Two selections are different when they take different positions, even if the throws taken read the same.

Return how many non-empty selections score exactly k.

Examples

Example 1

Input
nums = [2, 3, 4, 6, 1, 5], k = 24
Output
4

Selections scoring 24 include the throws 4 and 6, the throws 2, 3 and 4, and each of those again with the throw of 1 taken as well, since taking it changes nothing about the product but does make a different selection.

Example 2

Input
nums = [1, 1, 1], k = 1
Output
7

Any non-empty selection of throws of one scores one, and there are seven of them.

Example 3

Input
nums = [6], k = 5
Output
0

The only selection scores 6, so nothing scores 5.

Constraints

  • 1 <= nums.length <= 19
  • 1 <= nums[i] <= 6
  • 1 <= k <= 10^15

The signature

The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

Python
def count_sequences(nums: list[int], k: int) -> int:
Java
public int countSequences(int[] nums, long k)
September 7
Apply