Under construction

Still being built. Things may change

Elias Omega Coding

Time limit1sMemory limit128 MB

Summary
For each positive integer until a terminating 0, output its Elias omega code, built by recursively prepending the code of the bit length.
Level

Easy3 of 10

Topics
Bit manipulation, Recursion, Implementation, Math
Solved
No attempts yet

Problem

Elias codes are prefix codes for encoding positive integers efficiently when nothing is known in advance about how large the integers will be, and when a smaller integer is assumed to be at least as likely as a larger one. For this problem we bound the magnitude of the integers, and every integer to encode is greater than 11.

Elias defined three variants — Elias gamma, Elias delta, and Elias omega. This problem introduces the gamma and omega variants and asks you to implement the Elias omega code.

Let β(n)\beta(n) denote the binary representation of a positive integer nn (its leading bit is always 11), and let ∣β(n)∣|\beta(n)| be its number of bits. If a receiver already knew ∣β(n)∣|\beta(n)|, the sender could transmit β(n)\beta(n) alone. When the receiver does not know it, the length must first be sent in a self-delimiting way; the variants differ in how that length is encoded.

Elias gamma. A positive integer nn is written as two concatenated fields: a prefix of ⌊log⁡2n⌋\lfloor \log_2 n \rfloor zero bits, followed by β(n)\beta(n), which occupies ⌊log⁡2n⌋+1\lfloor \log_2 n \rfloor + 1 bits. For example β(9)=1001\beta(9) = 1001, so the gamma code of 99 is 00010010001001: the three leading zeros announce that four bits follow, and those four bits are β(9)\beta(9).

Elias omega. Instead of a plain run of leading zeros, the omega code encodes the length recursively. Writing ⌊log⁡2n⌋=∣β(n)∣−1\lfloor \log_2 n \rfloor = |\beta(n)| - 1, the omega code is defined by

code(n)={0 β(n),if ⌊log⁡2n⌋=1,code ⁣(⌊log⁡2n⌋) β(n),otherwise.\mathrm{code}(n) = \begin{cases} 0\,\beta(n), & \text{if } \lfloor \log_2 n \rfloor = 1, \\ \mathrm{code}\!\left(\lfloor \log_2 n \rfloor\right)\,\beta(n), & \text{otherwise.} \end{cases}

In other words, the recursion stops as soon as a stage needs only a single leading 00.

As an illustration, take 536870907536870907, whose binary form β(536870907)\beta(536870907) needs 2929 bits. Building the code stage by stage (with β(2)=10\beta(2)=10, β(4)=100\beta(4)=100, β(28)=11100\beta(28)=11100):

0    10    100    11100    β(536870907).0 \;\; 10 \;\; 100 \;\; 11100 \;\; \beta(536870907).

Concatenating everything yields the Elias omega code

0101001110011111111111111111111111111011,0101001110011111111111111111111111111011,

which can be uniquely decoded back to 536870907536870907.

Given several positive integers, output the Elias omega code of each.

Input

Each line contains one positive integer nn with 2≤n≤2,000,000,0002 \le n \le 2{,}000{,}000{,}000. A line containing 00 marks the end of the input and is not encoded. At most 100100 integers are given before the terminating 00.

Output

For each integer of the input (excluding the terminating 00), print its Elias omega code on its own line, in the same order. The output must contain no spaces and no blank lines.

Examples4

  1. Example 1

    Input
    2
    510
    7
    120000
    536870905
    49
    5
    0
    
    Expected output
    010
    0111000111111110
    010111
    0101001000011101010011000000
    0101001110011111111111111111111111111001
    010101110001
    010101
    
  2. Example 2

    Input
    2
    0
    
    Expected output
    010
    
  3. Example 3

    Input
    3
    0
    
    Expected output
    011
    
  4. Example 4

    Input
    4
    0
    
    Expected output
    010100