Data Structures & AlgorithmsHardICPC regional · counting DP
Problem: How many ordered sequences of jumps of sizes 2, 3 or 5 sum exactly to 12? Run the DP.
Problem: How many ordered sequences of jumps of sizes 2, 3 or 5 sum exactly to 12? Run the DP.
ways = [1] + [0] * 12
for v in range(1, 13):
for s in (2, 3, 5):
if v >= s:
ways[v] += ways[v - s]
print(ways[12])What is printed?- A. 22
- B. 18
- C. 27
- D. 25
Correct answer: C. 27
The ordered compositions of 12 into parts 2, 3 and 5 satisfy ways[v] = ways[v-2] + ways[v-3] + ways[v-5], which gives 27. Unordered counting would give a smaller number.
More Coding Interview Problems questions
- Problem: What is the size of the largest divisible subset of [1, 2, 4, 8, 3] (every pair must satisfy one dividing the other)? Run the…
- Problem: How many subarrays of [1, 2, 1, 2, 3] contain exactly 2 distinct values? Run the code using the 'at most' trick. python from…
- Problem: Dijkstra from 0 with edges (0,1,1), (1,3,2), (0,2,4), (2,3,1). Which vertex sequence is the shortest path from 0 to 3? Run the…
- Problem: Is the graph with edges forming the 5-cycle 0-1-2-3-4-0 bipartite? And is the 4-cycle 0-1-2-3-0 bipartite? Run the BFS colouring…
- Problem: Tree with edges 0-1, 0-2, 1-3, 1-4, 2-5. How many nodes are in the subtree of node 1 (rooted at 0)? Run the DFS code. python…
- Problem: Which statement about Fenwick (binary indexed) trees is false?