XOR Set Expansion
Time limit1sMemory limit128 MB
Given an initial integer set, count the expansion rounds that add XORs of current and original elements until the set stops growing.
- Level
Medium7 of 10
- Topics
- Bit manipulation, BFS, Math
- Solved
- No attempts yet
Problem
A set of numbers is given. The algorithm below widens the set until no new value shows up, and counts how many times it widened the set.
counter = 0
S = S0
loop:
S' = { a xor b : a in S, b in S0, a != b }
if every value of S' is already in S:
print counter and stop
S = S union S'
counter = counter + 1
goto loop
One round collects every result of XOR-ing a value already in with a value of the original set , and those results form . Equal values are never paired, so 0 stays out of . Once a round yields no new value at all, the algorithm prints the current counter and stops.
For = {1, 2, 4} the algorithm runs like this.
- start: counter = 0, S = {1, 2, 4}
- round 1: S' = {3, 5, 6}, S = {1, 2, 3, 4, 5, 6}, counter = 1
- round 2: S' = {1, 2, 3, 4, 5, 6, 7}, S = {1, 2, 3, 4, 5, 6, 7}, counter = 2
- round 3: S' = {1, 2, 3, 4, 5, 6, 7} holds no new value, so the algorithm prints 2 and stops.
A set with a single element has no pair to XOR, so is empty and the answer is 0.
Write a program that reads and computes the counter value the algorithm prints.
Input
The first line has a positive integer , the number of test cases. is at most 100,000.
Each test case takes two lines. The first line has , the size of the initial set , and is between 1 and 50. The second line has the elements of , separated by spaces. Every element is an integer between 1 and 500,000, and all elements are different.
Output
For each test case print one line in the form Case #x: R, where is the test case number starting at 1 and is the counter value the algorithm prints.