Binary String Ordering
Time limit1sMemory limit64 MB
Print all 2^N binary strings of length N in the order given by the binary reflected Gray code formula i XOR floor(i/2).
- Level
Easy3 of 10
- Topics
- Bit manipulation, Math, Implementation
- Solved
- No attempts yet
Problem
Nikola would not stop asking questions in math class, so his teacher gave the whole class this task.
There are binary strings of length . Print all of them, one per line, each string exactly once, so that the distance between every two neighboring lines is 1.
The distance between two binary strings is the number of positions where the corresponding digits differ. For example:
- distance(111, 000) = 3 (the strings differ in the first, second and third positions)
- distance(111100, 101010) = 3 (the strings differ in the second, fourth and fifth positions)
- distance(110011, 110011) = 0
Many orderings satisfy the condition, so this problem accepts only one of them. Counting lines from 0, line holds written as an digit binary number, where is bitwise exclusive or and the number is padded on the left with '0'. That ordering is the binary reflected Gray code, and every two neighboring lines differ in exactly one digit.
Input
The first and only line contains , the length of the binary strings. ()
Output
Print lines. Line () holds the digit binary representation of . Every line must consist of exactly digits, each '0' or '1'.
You may assume that at least one solution always exists.