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])
  1. A. 0
  2. B. 720
  3. C. 1
  4. 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

Exams that ask this topic