Data Structures & AlgorithmsHardICPC regional · counting DP
Problem: In how many ordered ways can 10 be written as a sum of parts from {1, 2, 3}, where the order of the parts matters? Run the code.
Problem: In how many ordered ways can 10 be written as a sum of parts from {1, 2, 3}, where the order of the parts matters? Run the code.
t = [1, 1, 2]
for n in range(3, 11):
t.append(t[n - 1] + t[n - 2] + t[n - 3])
print(t[10])What is printed?- A. 274
- B. 149
- C. 81
- D. 504
Correct answer: A. 274
The recurrence t(n) = t(n-1) + t(n-2) + t(n-3) counts the last part. Counting unordered partitions instead gives a much smaller number, which is the trap.
More Coding Interview Problems questions
- Problem: How many subsets (including the empty subset) of [1, 2, 3] have XOR equal to 0? Run the code. python a = [1, 2, 3] print(sum(1…
- Problem: What does the following print? python print(bin(10).count('1'), (10).bitlength())
- Problem: How many pairs (i < j) of [2, 3, 4, 5, 6] have gcd(a[i], a[j]) = 1? Run the code. python from math import gcd a = [2, 3, 4, 5, 6]…
- What does this Python program print? python a = [2, -2, 3, -3, 1, 1] p = {0: 1}; s = 0; ans = 0 for x in a: s += x ans += p.get(s, 0) p[s]…
- What does this program print? python from collections import Counter a = [3, 1, 2, 3, 1, 2, 3] c = Counter(a) pairs = sum(v (v - 1) // 2…
- What does this program print? python a = [6, 3, 8, 1] mx = [0] len(a) mx[-1] = a[-1] for i in range(len(a) - 2, -1, -1): mx[i] = max(a[i]…