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 Rust (1.15.1) (judge verdict: Accepted)



#[allow(dead_code)]
pub struct Scanner<'a> {
    _buf: &'a str,
    iter: ::std::str::SplitWhitespace<'a>,
}

#[allow(dead_code)]
impl<'a> Scanner<'a> {
    pub fn new(buf: &'a str) -> Self {
        Scanner {
            _buf: buf,
            iter: buf.split_whitespace(),
        }
    }

    pub fn array<T>(&mut self, n: usize) -> Vec<T>
        where T: ::std::str::FromStr,
              T::Err: ::std::fmt::Debug,
    {
        let mut res = Vec::with_capacity(n);
        for _ in 0..n {
            res.push(self.value());
        }
        res
    }

    pub fn value<T>(&mut self) -> T
        where T: ::std::str::FromStr,
              T::Err: ::std::fmt::Debug,
    {
        self.next().parse().unwrap()
    }

    pub fn chars(&mut self) -> Vec<char> {
        self.next().chars().collect()
    }

    fn next(&mut self) -> &str {
        self.iter.next().unwrap()
    }
}

// n ビットで表せる最大値
fn fmax(n: i64) -> i64 {
    assert!(n > 0);
    let m = (n-1) / 2;
    (4_i64.pow(m as u32+1) - 1) / 3
}

// n ビットで表せる最小値
fn fmin(n: i64) -> i64 {
    assert!(n > 0);
    if n == 1 { return 0; }
    let m = n / 2;
    -2 * (4_i64.pow(m as u32) - 1) / 3
}

fn main() {
    let mut _input = String::new();
    {
        use ::std::io::Read;
        ::std::io::stdin().read_to_string(&mut _input).unwrap();
    }
    let mut sc = Scanner::new(&_input);

    let mut n = sc.value::<i64>();

    assert_eq!( 1, fmax(1));
    assert_eq!( 1, fmax(2));
    assert_eq!( 5, fmax(3));
    assert_eq!( 5, fmax(4));
    assert_eq!(21, fmax(5));
    assert_eq!(  0, fmin(1));
    assert_eq!( -2, fmin(2));
    assert_eq!( -2, fmin(3));
    assert_eq!(-10, fmin(4));
    assert_eq!(-10, fmin(5));

    let mut ans = String::new();
    let mut bits_prev = -1;
    loop {
        let mut bits = -1;
        for i in 1.. {
            if fmin(i) <= n && n <= fmax(i) {
                bits = i;
                break;
            }
        }

        if bits_prev != -1 {
            for _ in 0..(bits_prev-bits-1) {
                ans += "0";
            }
        }

        if bits == 1 {
            if n == 0 {
                ans += "0";
            }
            else {
                ans += "1";
            }
            break;
        }

        ans += "1";
        n -= (-2_i64).pow(bits as u32-1);
        bits_prev = bits;
    }

    println!("{}", ans);
}