#P8553. Gray Permutation
Gray Permutation
Gray Permutation
You are given two distinct non‐negative integers \(l\) and \(r\) with \(l < r\). Consider the sequence \(a\) of length \(r-l+1\) which is a permutation of the set \(\{l, l+1, \dots, r\}\). The sequence \(a\) is said to be a Gray permutation if for every \(1 \le i \le r-l\) the following condition holds:
[ \operatorname{popcount}(a_i \oplus a_{i+1}) = 1, ]
where \(\operatorname{popcount}(x)\) denotes the number of 1's in the binary representation of \(x\) and \(\oplus\) denotes the bitwise XOR operation. In other words, consecutive elements in \(a\) differ in exactly one bit when written in binary.
Your task is to output any Gray permutation of \(\{l, l+1, \dots, r\}\) if one exists, or output -1
if no such permutation exists.
Note: Since every edge (i.e. a step from one number to another) in the implicit graph connecting numbers that differ in exactly one bit always connects numbers of opposite parity (when considering the parity of the number of 1's in their binary representation), a necessary condition for the existence of a Gray permutation is that the two parts (even and odd parity) must either have equal sizes (if \(r-l+1\) is even) or differ by exactly one (if \(r-l+1\) is odd).
inputFormat
The first and only line of input contains two space-separated non-negative integers \(l\) and \(r\) (with \(l < r\)).
Constraints: It is guaranteed that \(r-l+1\) is sufficiently small (for example, \(r-l \le 16\)) so that a brute force search is feasible.
outputFormat
If a valid Gray permutation exists, output the permutation \(a_1, a_2, \dots, a_{r-l+1}\) (space-separated). Otherwise, output -1
(without quotes).
sample
0 1
0 1