Problem:
Score : 300 points

Problem Statement

Given an integer N, find the base -2 representation of N.

Here, S is the base -2 representation of N when the following are all satisfied:

S is a string consisting of 0 and 1.

Unless S = 0, the initial character of S is 1.

Let S = S_k S_{k-1} ... S_0, then S_0 \times (-2)^0 + S_1 \times (-2)^1 + ... + S_k \times (-2)^k = N.

It can be proved that, for any integer M, the base -2 representation of M is uniquely determined.

Constraints

Every value in input is integer.

-10^9 \leq N \leq 10^9

Input

Input is given from Standard Input in the following format:

N

Output

Print the base -2 representation of N.

Sample Input 1

-9

Sample Output 1

1011

As (-2)^0 + (-2)^1 + (-2)^3 = 1 + (-2) + (-8) = -9, 1011 is the base -2 representation of -9.

Sample Input 2

123456789

Sample Output 2

11000101011001101110100010101

Sample Input 3

0

Sample Output 3

0

Solution in Ruby (2.7.1) (judge verdict: Accepted)

#!/usr/bin/env ruby

require 'set'

class Hash
  def push(key, value)
    self[key] = [] if self[key] == nil
    self[key].push(value)
  end
end

class Array
  def lower_bound(value)
    left = -1;
    right = self.length;
    while left + 1 < right
      mid = left + (right - left) / 2;
      if self[mid] >= value
        right = mid
      else
        left = mid
      end
    end
    right
  end

  def unique
    res = [self.first]
    each_cons(2) do |a, b|
      if a != b
        res.push(b)
      end
    end
    res
  end
end

def get_ints
  gets.chomp.split.map(&:to_i)
end

def get_ints_minus_one
  get_ints.map { |x| x - 1 }
end

def get_int
  gets.chomp.to_i
end

N = get_int

n_abs = N.abs
@plus = []
x = 0
i = 0
while x < n_abs
  x += 4 ** i
  @plus << x
  i += 1
end

@minus = []
x = 0
i = 0
while x < n_abs
  x += 2 * 4 ** i
  @minus << x
  i += 1
end

def check(n)
  return [-1, n] if n == 1 || n == 0

  if n > 0
    index = @plus.bsearch_index { |x| x >= n }
    [index, n - 4 ** index, 'plus']
  else
    index = @minus.bsearch_index { |x| x >= n.abs }
    [index, n + 2 * 4 ** index, 'minus']
  end
end

ans = []
test = N
loop do
  stock = check(test)
  ans << stock
  test = stock[1]
  break if stock[0] < 0
end

result = []
ans.each do |index, v, type|
  if type == 'minus'
    result[2 * (index+1)-1] = '1'
  elsif type == 'plus'
    result[2 * index] = '1'
  else
    result[0] = v
  end
end

puts result.map { |x| x == nil ? '0' : x }.reverse.join