Data Structures & AlgorithmsHardCodeforces Div 2 C · ICPC regional
What does this program print?
What does this program print?
n = 6
dp = [0] * (1 << n)
dp[0] = 1
for mask in range(1 << n):
if dp[mask] == 0: continue
for v in range(n):
if not (mask >> v) & 1:
dp[mask | (1 << v)] += dp[mask]
print(dp[(1 << n) - 1])- A. 0
- B. 720
- C. 1
- D. 1440
Correct answer: B. 720
dp[mask] counts orderings of the values in mask, so the full mask holds n! orderings. The program prints 6! = 720; resetting the start state to zero or assigning instead of adding breaks the count.
More Coding Interview Problems questions
- What does this program print? python x = 22 cnt = 0 while x: x &= x - 1 cnt += 1 print(cnt, bin(22).count('1'))
- Array [2, 7, 4, 9, 5, 8]. What is the largest sum of a contiguous subarray having at most 3 elements?
- Points (0,0), (1,2), (2,1), (3,3), (4,0) in the plane. How many unordered pairs have Manhattan distance exactly 3?
- Four sets {1,2,3}, {2,4}, {3,4,5}, {1,5} are given. How many non-empty sub-collections of these sets have a union equal to {1, 2, 3, 4, 5}?
- Problem: How many pairs of equal values (i < j, a[i] = a[j]) does a = [1, 2, 1, 3, 2, 2] contain? Run the code. python from collections…
- Problem: How many longest increasing subsequences does [1, 3, 5, 4, 7] have? Run the DP that counts both length and number of optimal…