Data Structures & AlgorithmsVery hard
Why does the count of subsequences with sum divisible by m give the same answer if you replace each value by its residue mod m?
- A. The answer always becomes zero.
- B. Residues change the set of subsets.
- C. Residues are only valid for prime m.
- D. Subset sums depend only on residues, since (a + m) and a have the same residue, so the divisibility test is unchanged.
Correct answer: D. Subset sums depend only on residues, since (a + m) and a have the same residue, so the divisibility test is unchanged.
Divisibility by m depends only on the total mod m, so each value can be reduced first without changing which subsets qualify. This is the reason residue DPs are valid.
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- Which statement about the prefix product Pi and suffix product Si modulo a prime p is correct when a zero element is present?
- Evaluate the integer expression 2(3+4) - 5/(1+1) with integer division (truncation toward zero). What is the result?
- What is the minimum number of brackets that must be removed from ')()())(()' to make it a balanced sequence?
- In a 3 by 3 grid with no walls, how many cells are exactly 2 moves (up, down, left or right) away from the top-left corner?
- How many index pairs i < j in [3, 4, 4, 5, 7, 6] have values that differ by exactly 1?
- What is the length of the longest contiguous subarray of [1, 2, 1, 2, 3] that contains at most two distinct values?