Binary Encoding

Interview

Time limit1sMemory limit128 MB

Summary
Given m, output the truncated binary code for each integer from 0 to m-1 following the standard truncated binary encoding rules.
Level

Easy3 of 10

Topics
Bit manipulation, Implementation, Math
Solved
No attempts yet

Problem

Binary encoding represents each integer from 00 to 2n−12^n - 1 using exactly nn bits, where every bit is 00 or 11. The most significant bit is written first, and two codes are compared from left to right, from the most significant bit to the least significant one. Codes are assigned so that when the numbers are listed in ascending order, their codes are also in ascending order. For example, the binary encoding of the numbers 00 to 77 is:

NumberCodeNumberCode
00004100
10015101
20106110
30117111

Truncated binary encoding generalizes this to represent the integers from 00 to m−1m - 1, where mm need not equal 2n2^n for any nn. Unlike ordinary binary encoding, it may use a different number of bits for different numbers. Let nn be the smallest integer with m≤2nm \le 2^n. Then truncated binary encoding represents each number using either nn or n−1n - 1 bits. Some numbers use only n−1n - 1 bits, and this happens only when m<2nm < 2^n; when m=2nm = 2^n the encoding is the same as ordinary binary encoding. Truncated binary codes are compared from left to right, just like binary codes.

Truncated binary encoding also satisfies the following rules:

  • Smaller numbers use the same number of bits as, or fewer bits than, larger numbers.
  • When the numbers are listed in ascending order, their codes are also in ascending order.
  • The codes for the numbers 00 to m−1m - 1 are all distinct.
  • No code with n−1n - 1 bits is a prefix of any code with nn bits.
  • The total number of bits used for the numbers 00 to m−1m - 1 is minimal.

For example, the truncated binary encoding of the numbers 00 to 55 is:

NumberCodeNumberCode
0004110
1015111
2100
3101

Encode the numbers from 00 to m−1m - 1 using truncated binary encoding.

Input

The input contains a single integer mm (2≤m≤100002 \le m \le 10000).

Output

Print mm lines. For each ii from 00 to m−1m - 1, the line at position ii must contain the truncated binary code of the number ii, so the codes are printed in ascending order of the numbers they represent.

Examples1

  1. Example 1

    Input
    6
    
    Expected output
    00
    01
    100
    101
    110
    111