Problem 524029 · hard · Phase 05 Advanced Algorithms & Graphs

Ranked Keys From the Scrambler Chips

bit manipulation · xor basis · gaussian elimination · binary representation

A radio has scrambler chips; chip i holds the code chips[i]. Plugging in any sub-collection of the chips (possibly none, possibly all) produces the key equal to the XOR of their codes. Plugging in no chip gives key 0. Different sub-collections can give the same key.

Sort all distinct keys that can be produced in increasing order and number them from 1. For each k in queries, report the key with number k, or -1 if fewer than k distinct keys exist. Return the answers as a list in query order.

Examples

Input:  chips = [5, 3], queries = [1, 2, 3, 4, 5]
Output: [0, 3, 5, 6, -1]
Explanation: the keys are 0, 5, 3 and 5 ^ 3 = 6; sorted: 0, 3, 5, 6.
Input:  chips = [6, 6, 0], queries = [2, 3]
Output: [6, -1]

Constraints

  • 0 <= len(chips) <= 2 * 10**4, 0 <= chips[i] < 2**50
  • 0 <= len(queries) <= 2 * 10**4, 1 <= queries[j] <= 2**60
  • The number of distinct keys can be astronomically large; listing them is not an option.

Goals

  • Reduce a list of numbers to an XOR basis that spans the same values
  • Put the basis in reduced form so each leading bit belongs to one vector
  • Read the k-th smallest reachable value off the binary digits of k - 1
Starting Python…