Problem 537715 · medium · Phase 05 Advanced Algorithms & Graphs

Count Ways to Reach a Score

backtracking · counting · combination sum

In a target-throwing game each throw scores one of the values in moves (distinct positive integers). Throws can be repeated any number of times, and the order of throws does not matter: the game only records how many throws of each value were made. Return the number of different ways to finish with exactly total points.

Examples

Input:  moves = [2, 3, 7], total = 12
Output: 4
Explanation: {2,2,2,2,2,2}, {2,2,2,3,3}, {3,3,3,3}, {2,3,7}.

Input:  moves = [4], total = 6
Output: 0

Input:  moves = [5, 10], total = 0
Output: 1
Explanation: throwing nothing is the one way to score 0.

Constraints

  • 1 <= len(moves) <= 8, 1 <= moves[i] <= 40, distinct
  • 0 <= total <= 40

Goals

  • Count solutions instead of collecting them
  • Use a non-decreasing order so each multiset is counted once
Starting Python…