Natural XOR elements

3.3

69 votes
Bit Manipulation, Basic Programming, Basics of bit manipulation, XOR, Basics of Bit Manipulation
Problem

You are given an integer N. To solve the problem, you must find the minimum number of elements that must be removed from the set S={1,2,...,N} such that the bitwise XOR of the remaining elements is 0.

Input format

  • The first line contains an integer t(1t105) representing the number of test cases.
  • The first and only line of each test case contains an integer N(1N109) representing the number presented to you.

Output format

For each test case, print a single line.

The first integer k represents the minimum number of elements and you must remove from S. Then, k space-separated integers a1,a2,...,ak follows representing the elements that you have to remove from S.

If there are multiple possible solutions, output the lexicographically largest one. A solution a1,a2,...,ak is lexicographically larger than solution b1,b2,...,bk, if ai>bi where i is the smallest index where aibi

It can be proved that in an optimal solution k31.

Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

After removing 1 there are no elements left. Therefore XOR of the remaining elements becomes 0.

After removing 1 and 2 there are no elements left. Therefore XOR of the remaining elements becomes 0.

We don't need to remove any element as XOR of 1, 2, and 3 is already 0.

After removing 4 we are left with 1,2 and 3 whose XOR is 0.

 

Editor Image

?