Data Structures & AlgorithmsVery hardICPC regional · bitmask round
Problem: Count the subsets (including the empty subset) of {1, 2, 3, 4, 5, 6} whose sum is divisible by 3.
Problem: Count the subsets (including the empty subset) of {1, 2, 3, 4, 5, 6} whose sum is divisible by 3.
a = [1, 2, 3, 4, 5, 6]
cnt = 0
for m in range(1 << len(a)):
if sum(a[i] for i in range(len(a)) if m >> i & 1) % 3 == 0:
cnt += 1
print(cnt)What is printed?- A. 24
- B. 20
- C. 28
- D. 32
Correct answer: A. 24
Group by residues mod 3: {3,6} are free (4 choices), and the residue-1/2 pieces {1,4} and {2,5} give 6 combinations that sum to 0 mod 3. Total 6 * 4 = 24.
More Coding Interview Problems questions
- Problem: How many integers x with 1 <= x <= 100 have no two adjacent equal decimal digits? Use the rule that a one-digit number always…
- Problem: What is the sum of all divisors of 360 = 2^3 3^2 5?
- Problem: Find the smallest positive x satisfying x ≡ 2 (mod 3), x ≡ 3 (mod 5) and x ≡ 2 (mod 7).
- Problem: Which pair (x, y) satisfies 240x + 46y = gcd(240, 46)?
- Problem: Cows must be placed in stalls at positions [1, 2, 4, 8, 9] so that the minimum distance between any two cows is as large as…
- Problem: Trains arrive at times [900, 940, 950, 1100, 1500, 1800] and depart at [910, 1200, 1120, 1130, 1900, 2000]. The two-pointer code…