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



Sample input:
-9


Sample output:
1011

Submission status: Accepted

Solution code:
n = int(input())

a = []
s = 0
while (-2)**s<10000000000:
    a.append((-2)**s)
    s += 1


def judge(n):
    sum = 0
    count = 0
    dig = 0
    if n >0:
        for num in a:
            count += 1
            if num >0:
                sum += num
            if sum >= n:
                dig = count
                break
    else:
        for num in a:
            count += 1
            if num <0:
                sum += num
            if sum <= n:
                dig = count
                break
            
    return dig
    
ans = ["0" for i in range(judge(n))]


while n!=0:
    ans[judge(n)-1] = "1"
    n -= a[judge(n)-1]

    
answer = ""
for i in range(len(ans)-1,-1,-1):
    answer += ans[i]
    
print(answer)

Submission status: Accepted

Solution code:
num=[]
n=int(input())
while n!=0:
    if abs(n)%2==1:
        num.append("1")
        n=(n-1)//(-2)
    else:
        num.append("0")
        n=n//(-2)
if len(num)==0:
    num=["0"]
print("".join(num[::-1]))

Submission status: Accepted

Solution code:
n = int(input())
ans = ""

while True:
    if n % 2 == 0:
        ans = '0' + ans
    else:
        ans = '1' + ans
        n -= 1
    
    n = -(n / 2)
    if n == 0:
        break

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
a = [0]*32
i = 0
ans = 0

if(N==0):	print(0)

else:
  while N != 1 :
    if(N%2==0):
      a[i] = 0
      N=N/(-2)
      i = i+1
    else:
      a[i] = 1
      N = (N-1)/(-2)
      i = i+1
  a[i] = 1
  for k in range(i+1):
    ans = ans + a[k] * 10**k
  print(ans)

Submission status: Accepted

Solution code:
N = int(input())


def convert(n, b):
    if n == 0:
        return "0"
    out = []
    while n != 0:
        n, rem = divmod(n, b)
        if rem < 0:
            n += 1
            rem -= b
        out.append(rem)
    return "".join(map(str, out[::-1]))


print(convert(N, -2))

Submission status: Accepted

Solution code:
n = int(input())

ans = '' if n != 0 else '0'
while n != 0:
    ans = str(((n % (-2) + 2) % (2))) + ans
    n //= 2
    n *= -1
print(ans)

Submission status: Accepted

Solution code:
a=int(input())
sum=0
s=''
'''if a<0:
    b = abs(a)
    if b%2==1:
        b=b+1
        print(str(bin(b))[2:-1]+'1')
    else:
        print(str(bin(b))[2:-1])'''
if a!=0:
    while(a!=1):
        if a%(-2)==-1:
            s=s+'1'
            a=a-1
            a=a/-2
        else:
            s = s + '0'
            a=a/-2
    s=s+'1'
    for i in range(len(s)):
        print(s[len(s)-1-i],end='')
    print()
else:
    print(0)

Submission status: Accepted

Solution code:
from sys import stdin


def main():
    N = int(stdin.readline().rstrip())

    if N == 0:
        print('0')
        return

    ans = ''
    base = 2
    while N != 1:
        ans = str(abs(N % base)) + ans
        N = N // base
        N = -1 * N
    print('1' + ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import math

N = int(input())



ans = ''
i = 0


while True:
	a = math.ceil(N/-2)
	p = N - (-2 * a)
	if p == 1:
		ans = '1' + ans 
	else:
		ans = '0' + ans 
	N = a
	if N == 0:
		break


print(ans)

Submission status: Accepted

Solution code:
import math
N = int(input())
a = math.ceil(N/(-2))
r = 0
s = ""
if N == 0:
    print(0)
else:
    while a != 1:
        r = N-a*(-2)
        s = str(r) + s
        N = a
        a = math.ceil(N/(-2))
    if a == 1:
        r = N-a*(-2)
        s = str(r) + s
        s = "1" + s   
    print(s)

Submission status: Accepted

Solution code:
N = int(input())
l = [0]*31
for i in range(0,30):
    l[i+1] = l[i] + 4**i

mlist = [0] * 61
for i in range(30):
    mlist[i*2+1] = l[i]
    mlist[i*2] = l[i]
mlist = mlist[1:-1]

def zojudge(N, d):
    if ((N-1) // 2**d) % 2 == 0:
        return "1"
    else:
        return "0"

L = [zojudge(N - m, i) for (i, m) in enumerate(mlist)]
S = int("".join(reversed(L)))
print(S)

Submission status: Accepted

Solution code:
import sys

N = int(input())
m = 1

min = 0
max = 1

if N == 0:
    print("0")
    sys.exit()
elif N == 1:
    print("1")
    sys.exit()

i = 0
while True:
    m *= 2

    if i % 2 == 0:
        min -= m
    else:
        max += m

    if min <= N <= max:
        break

    i += 1

# print(min,max)
# print("i =",i)
# print("m =",m)
ans = []

while True:
    if (min + max)/2 < N:
        if i % 2 == 0:
            ans.append("0")
        else:
            ans.append("1")
        min += m

    elif N < (min + max)/2 :
        if i % 2 == 0:
            ans.append("1")
        else:
            ans.append("0")
        max -= m

    else:
        break

    m /= 2
    i -= 1

print("".join(ans))

Submission status: Accepted

Solution code:
N=int(input().strip())

def getMaxBit(x):
    if x==0:
        return None
    elif x>0:
        s=0
        i=0
        while s<x:
            s+=2**i
            i+=2
        return i-2
    else:
        s=0
        i=1
        while s>x:
            s-=2**i
            i+=2
        return i-2
ans=set()
while N!=0:
    a=getMaxBit(N)
    ans.add(a)
    N-=(-2)**a
S=""
if len(ans):
    for i in range(max(ans)+1):
        S=("1" if i in ans else "0")+S
else:
    S="0"
print(S)

Submission status: Accepted

Solution code:
n = int(input())
if n==0:
  print("0")
  exit()
s=""
t=-2
while n != 0:
  z = 0 if n % t ==0 else 1
  s = str(z)+s
  n = n - z*t/(-2)
  t = t*(-2)
print(s)

Submission status: Accepted

Solution code:
n = int(input())

if n == 0:
    print('0')
else:
    ans = []
    x = n
    while x != 1:
        if x % -2 == 0:
            ans.append('0')
            x //= -2
        else:
            ans.append('1')
            x //= -2
            x += 1
    ans.append('1')
    t_ans = ''
    for i in range(len(ans)-1, -1, -1):
        t_ans += ans[i]
    print(t_ans)

Submission status: Accepted

Solution code:
n=int(input())
s=''
if n==0:
  print('0')
  exit()
while(n!=0):
  m=abs(n)
  if m%2==1:
    n-=1
    n=n//(-2)
    s='1'+s
  else:
    n=n//(-2)
    s='0'+s
print(s)

Submission status: Accepted

Solution code:
N = int(input())
ans = [0]*36

def bit(inp,ans):
    if inp == 0:
        ans[0] = 0
        inp2 = 0
    
    if inp > 0:
        b = [1,5,21,85,341,1365,5461,21845,87381,349525,1398101,5592405,22369621,89478485,357913941,1431655765]
        pcount = 0
        for ii in range(len(b)):
            if inp <= b[ii]:
                inp2 = inp - 4**ii
                break
            pcount = pcount + 1
        ans[pcount*2] = 1
        
    if inp < 0:
        b2 = [-2,-10,-42,-170,-682,-2730,-10922,-43690,-174762,-699050,-2796202,-11184810,-44739242,-178956970,-715827882,-2863311530]
        pcount = 0
        for ii in range(len(b2)):
            if inp >= b2[ii]:
                inp2 = inp + 2 * (4**ii)
                break
            pcount = pcount + 1
        ans[pcount*2+1] = 1
        
    return inp2,ans

    
while N != 0:
    N,ans = bit(N,ans)

fi = ans[::-1]
fistr = [str(n) for n in fi]
m = ''.join(fistr)
mm = int(m)

print(mm)

Submission status: Accepted

Solution code:
N = int(input())
S = ""
for i in range(1000):
    if N%((-2)**(i+1))==0:
        S="0"+S
        if N==0:
            break
    else:
        S="1"+S
        N-=(-2)**i
        if N==0:
            break
print(S)

Submission status: Accepted

Solution code:
N = int(input())
S = []
if N == 0:
    print('0')
    exit()
while N != 1:
    if N % (-2) == -1:
        S.append(1)
        N = (N - 1) // -2
    else:
        S.append(0)
        N = N // -2
S.append(1)
ans = ''
for s in S[::-1]:
    ans += str(s)
print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
n = int(input())
pos = [ 0 ] * 64
neg = [ 0 ] * 64
for i in range(32):
    if n >= 0:
        if n & (1 << i):
            pos[i] += 1
    else:
        if (- n) & (1 << i):
            neg[i] += 1
for i in range(32):
    delta = min(pos[i], neg[i])
    pos[i] -= delta
    neg[i] -= delta
    if i % 2 == 1:
        if pos[i]:
            pos[i + 1] += pos[i]
            neg[i] += pos[i]
            pos[i] = 0
        neg[i + 1] += neg[i] // 2
        neg[i] %= 2
    else:
        if neg[i]:
            neg[i + 1] += neg[i]
            pos[i] += neg[i]
            neg[i] = 0
        pos[i + 1] += pos[i] // 2
        pos[i] %= 2
s = ''
for i in reversed(range(64)):
    if pos[i] or neg[i]:
        s += '1'
    else:
        s += '0'
print(s.lstrip('0') or '0')

Submission status: Accepted

Solution code:
n = int(input())
b = -2

remains = []
if n == 0:
    print(0)
else:
    while n != 0:
        remain = n%b
        n = n//b

        if remain<0:
            remain += -b
            n += 1
        remains.append(remain)

    remains = list(map(str,remains))
    ans = ''.join(remains)[::-1]
    print(ans)

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    if n==0:
        return 0

    ans,up = '',0
    flg = 0
    if n<0:
        flg = 1
        n = -n

    for i in range(40):
        d = (n>>i&1)+up
        if i%2==flg:
            if d==0:
                ans,up = '0'+ans,0
            elif d==1:
                ans,up = '1'+ans,0
            elif d==2:
                ans,up = '0'+ans,1
            else:
                assert(False)
        else:
            if d==0:
                ans,up = '0'+ans,0
            elif d==1:
                ans,up = '1'+ans,1
            elif d==2:
                ans,up = '0'+ans,1
            else:
                assert(False)

    return ans[ans.find('1'):]

if __name__ == '__main__':
    print(main())

Submission status: Accepted

Solution code:
N = int(input())
if N == 0:
        print("0")
else:
        ans = []
        while N != 0:
                if N%2 == 1:
                        ans.append(1)
                        N -= 1
                        N = N//(-2)
                else:
                        ans.append(0)
                        N = N//(-2)
        ans_str = ""
        for i in ans:
                ans_str = str(i) + ans_str
        print(ans_str)

Submission status: Accepted

Solution code:
n=int(input())
a=[]
t=n
while(abs(t)>0):
    a.append(abs(t%(-2)))
    if t%(-2)==0:
        t//=-2
    else:
        t//=-2
        t+=1
a=list(map(str, a))
if n==0:
    print(0)
else:
    print("".join(a[::-1]))

Submission status: Accepted

Solution code:
N=int(input())
L=[]
M=""
while N!=0:
  M=int((-1)*(N%(-2)))
  if N<0:
    N=int((N-1)/(-2))
  else:
    N=int(N/(-2))
  L.insert(0,str(M))
if len(L)==0:
  L=["0"]
print("".join(L))

Submission status: Accepted

Solution code:
n = int(input())
if n==0:
    print(0)
    exit()
ans = []
while n!=0:
    t = n%(-2)
    n //= -2
    if t==-1:
        n += 1
        t *= -1
    ans.append(str(t))
print(''.join(ans[::-1]))

Submission status: Accepted

Solution code:
n = int(input())
ans = '00' if n == 0 else '0'
i = 1
pm = 1
while n != 0:
    a = 2**i
    b = abs(n) % a
    if b == 0:
        ans = ans + '0'
    else:
        ans = ans + '1'
        n -= pm*a/2
    pm = -pm
    i += 1
print(int(ans[i:0:-1]))

Submission status: Accepted

Solution code:
n = int(input())

def plus(k):
    j = 0
    while (2**(j+2) - 1)/3 < k:
        j += 2
    return 10**j,k-2**j

def minus(k):
    j = 1
    while ((-2)**(j+2)-(-2))/3 > k:
        j += 2
    return 10**j,k-(-2)**j

ans = 0    
while n != 0:
    if n > 0:
        a,n = plus(n)
        ans += a
    else:
        a,n = minus(n)
        ans += a

print(ans)

Submission status: Accepted

Solution code:
from functools import reduce

n = int(input())

if n == 0:
  print(0)
  exit()

res = []

while True:
  if n == 0:
    break
  if n == 1:
    res.append(1)
    break
  if n%2 == 0:
    res.append(0)
    n //= -2
  else:
    res.append(1)
    n = (n-1)//(-2)


print(reduce(lambda s, x: s + str(x), reversed(res) ,""))

Submission status: Accepted

Solution code:
N = int(input())

ans = ''

def divide(n):
	if n % (-2) != 0:
		p = (n//(-2))+1
		q = n - p * (-2)
	else:
		p = n//(-2)
		q = n - p * (-2)
	return p,q

while True:
	p,q = divide(N)
	if p == 0:
		ans = str(p) + ans
		break
	N = p
	ans = str(q) + ans
	if p == 1:
		ans = str(p) + ans
		break
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
if n == 0:
    print(0)
    exit(0)

oddsum = [2, 10, 42, 170, 682, 2730, 10922, 43690, 174762, 699050, 2796202, 11184810, 44739242, 178956970, 715827882, 2863311530]
evensum = [1, 5, 21, 85, 341, 1365, 5461, 21845, 87381, 349525, 1398101, 5592405, 22369621, 89478485, 357913941, 1431655765]
two = [1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536, 131072, 262144, 524288, 1048576, 2097152, 4194304, 8388608, 16777216, 33554432, 67108864, 134217728, 268435456, 536870912, 1073741824, 2147483648, 4294967296, 8589934592, 17179869184]
ans = 0
while n != 0:
    if n < 0:
        x = 1
        for i in oddsum:
            if n >= -1 * i:
                break
            x += 2
        n += two[x]
    else:
        x = 0
        for i in evensum:
            if n <= i:
                break
            x += 2
        n -= two[x]
    v = 1 << x
    ans += v
print(bin(ans)[2:])

Submission status: Accepted

Solution code:
N = int(input())

if N == 0:
    print("0")
else:
    r = ''
    while N != 0:
        N, rem = divmod(N, -2)
        if rem < 0:
            N += 1
            rem -= -2
        r = str(rem) + r
    print(r)

Submission status: Accepted

Solution code:
X=int(input())
if X==0:
	print(0)
else:
	A = []
	while(X!=0):
		X,R = divmod(X,-2)
		if (R<0):
			R-=(-2)
			X+=1
		A.append(R)
	print("".join(map(str,A[::-1])))

Submission status: Accepted

Solution code:
n = int(input()) * -1 # なぜか-1した数になる

s = ""
while n != 0:
    s = str(n % 2) + s
    n = n // -2

if s:
    print(s)
else:
    print(0)

Submission status: Accepted

Solution code:
N = int(input())
k=0
j=0
neg = 1
if N > 1:
	M = -2*N
	neg = 0
else:
	M = N
for i in range(-1*M):
	j = j - 2**(2*i + 1)
	if j <= M:
		k = i+1
		M = N-j
		break
	
out = ""
for i in range(2*k):
	if i%2 == 0:
		pl = M%2
		out = str(pl)+out
		M = int((M - pl)/2)
	else:
		pl = 1-(M%2)
		out = str(pl)+out
		M = int((M-1+pl)/2)

if N == 0:
	out="0"
elif out[0] == "0":
	out = out[1:]
	
print(out)

Submission status: Accepted

Solution code:
from collections import defaultdict
import sys,heapq,bisect,math,itertools,string,queue,datetime
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7
AtoZ = [chr(i) for i in range(65,65+26)]
atoz = [chr(i) for i in range(97,97+26)]

def inpl(): return list(map(int, input().split()))
def inpl_s(): return list(input().split())


def Base_10_to_n(X, n):

    if (int(X/n)):
        return Base_10_to_n(int(X/n), n)+str(X%n)
    return str(X%n)

N = int(input())

if N == 0:
	print(0)
	sys.exit()

ans = []
def calc(x):
	global ans

	if x%4 == 0:
		ans.append(0)
		ans.append(0)

	elif x%4 == 1:
		ans.append(1)
		ans.append(0)
		x -= 1
	elif x%4 == 2:
		ans.append(0)
		ans.append(1)
		x += 2
	elif x%4 == 3:
		ans.append(1)
		ans.append(1)
		x += 3

	return x//4

while N !=0:
	N = calc(N)


ans.reverse()
print(int(''.join(map(str,ans))))

Submission status: Accepted

Solution code:
n = int(input())

output = ""

num = 1

if n == 0:
    print(0)

else:
    while n != 0:
        # print("===")
        # print(num)

        if n % (-2 * num) == 0:
            output = "0" + output
        else:
            output = "1" + output
            n -= num

        num = num * (-2)
        # print(output)

    print(output)

Submission status: Accepted

Solution code:
N = int(input())
S = []
ans = "0"
while(abs(N) > 0):
  S.append(str(abs(N % (-2))))
  N += N % (-2)
  N = N // (-2)
  
S.reverse()

if S == []:
  print(ans)
else:
  ans = ""
  for i in S:
    ans += i
    
  print(ans)

Submission status: Accepted

Solution code:
N = int(input())
# N, K = map(int, input().split())
# c = list(map(int, input().split()))
# d = [list(map(int, input().split())) for _ in range(N)]
# s = input()


# ans = 0
# i = 0
#
# while N > 0:
#     s = N % 10
#     ans += pow(s*(-2), i)
#     N = N // 10
#     i = i + 1



ans = ""
n_array = []

if N == 0:
    ans = "0"

while N != 0:
    if N % 2 == 0:
        s = 0
    else:
        s = 1

    n_array.insert(0, s)

    if N > 0:
        N = (N // 2)*(-1)
    else:
        N = (-1)*N
        if N % 2:
            N += 1
        N = N // 2

maped_list = map(str, n_array)
for cc in maped_list:
    ans += cc


print(ans)

Submission status: Accepted

Solution code:
N= int(input())

s=''
while N!=0:
    if N%2==1:
        s='1'+s
        N-=1
    else:
        s='0'+s
    N=N//2 * -1
if s=='':
    s='0'
print(s)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

import math

def main():
    n = int(input())
    if n == 0:
        print('0')
        return

    bits = []
    while n != 0:
        qf = n / -2
        q = math.ceil(qf)
        m = n % -2
        bits.append('0' if m == 0 else '1')
        n = q

    print(''.join(reversed(bits)))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
t = n
k = 0
i = 0
ans = []
if n == 0:
    print(0)
else:
    while k != n:
        if i % 2 == 0:
            if t % 2 == 0:
                ans += "0"
            else:
                ans += "1"
                t -= 1
                k += (-2)**i
        else:
            if t % 2 == 0:
                ans += "0"
            else:
                ans += "1"
                t += 1
                k += (-2)**i
        t = int(t / 2)
        i += 1
    ans.reverse()
    print("".join(ans))

Submission status: Accepted

Solution code:
n = int(input())

s = []

a = n
b = 0

while (a !=0):
    if a%-2 ==0:
        b = a % -2
        a = a//-2
        s.insert(0,str(b))
    elif a%-2==-1:
        b = abs(a % -2)
        a = a//-2+1
        s.insert(0,str(b))

print(''.join(s) if len(s)!=0 else 0)

Submission status: Accepted

Solution code:
#!/usr/bin/env python
# coding: utf-8
#


def main(input_str):
    n = int(input_str)
    if n == 0:
        print(0)
        return
    chi = -2
    bai = -2
    dif = n
    ret = ""
    while dif != 0:
        #print(n % chi)
        #print("amari {} {}".format(dif % chi, chi))
        if 0 != dif % chi:
            # print('1',end="")
            ret = '1' + ret
            #print(1)
            dif -= chi/bai
        else:
            # print('0',end="")
            #print(0)
            ret = '0' + ret
        chi *= bai

    print(ret)
        
    
if __name__ == '__main__':
    try:
        main(input().strip())
    except EOFError:
        pass

Submission status: Accepted

Solution code:
N = input()
if N  == 0:
    print 0
elif N > 0:
    A = []
    B = []
    carry = 0
    cnt = 0
    while N > 0:
        if cnt == 0:
            B.append(0)
            if N % 2 == 1 and carry == 0:
                A.append(1)
            elif N % 2 == 1 and carry == 1:
                A.append(0)
            elif N % 2 == 0 and carry == 0:
                A.append(0)
            elif N % 2 == 0 and carry == 1:
                A.append(1)
                carry = 0
        else:
            A.append(0)
            if N % 2 == 1 and carry == 0:
                B.append(1)
                carry = 1
            elif N % 2 == 1 and carry == 1:
                B.append(0)
            elif N % 2 == 0 and carry == 0:
                B.append(0)
            elif N % 2 == 0 and carry == 1:
                B.append(1)
        N /= 2
        cnt ^= 1
    if carry:
        if cnt == 0:
            A.append(1)
            B.append(0)
        else:
            A.append(0)
            B.append(1)
            A.append(1)
            B.append(0)
    A.reverse()
    B.reverse()
    ans = []
    for a, b in zip(A, B):
        ans.append(a + b)
    print "".join(map(str, ans))
else:
    N *= -1
    A = []
    B = []
    carry = 0
    cnt = 0
    while N > 0:
        if cnt == 0:
            B.append(0)
            if N % 2 == 1 and carry == 0:
                A.append(1)
                carry = 1
            elif N % 2 == 1 and carry == 1:
                A.append(0)
            elif N % 2 == 0 and carry == 0:
                A.append(0)
            elif N % 2 == 0 and carry == 1:
                A.append(1)
        else:
            A.append(0)
            if N % 2 == 1 and carry == 0:
                B.append(1)
            elif N % 2 == 1 and carry == 1:
                B.append(0)
            elif N % 2 == 0 and carry == 0:
                B.append(0)
            elif N % 2 == 0 and carry == 1:
                B.append(1)
                carry = 0
        N /= 2
        cnt ^= 1
    if carry:
        if cnt == 1:
            B.append(1)
            A.append(0)
        else:
            B.append(0)
            A.append(1)
            B.append(1)
            A.append(0)
    A.reverse()
    B.reverse()
    ans = []
    for a, b in zip(A, B):
        ans.append(a + b)
    print "".join(map(str, ans))

Submission status: Accepted

Solution code:
def div(x):
    a = x //(-2)
    if a*(-2) > x:
        a+=1
    return a
n = int(input())
bi = ""
flag = 0
if n == 0:
    flag = 1
while n != 0:
    x = div(n)
    if n % (-2) != 0:
        bi = "1"+bi
    elif x != 0:
        bi = "0"+bi 
    n = x
print("0" if flag == 1 else bi)

Submission status: Accepted

Solution code:
N=int(input())
left,right=0,1
parity=1
interval=[]
ansList=[]
digit = -1
for i in range(40):
    if parity == 1:
        interval.append((right-2**i+1,right))
    else:
        interval.append((left,left+2**i-1))
    if left <= N and N <= right:
        digit = i
        break
    else:
        if parity==1:
            left -= 2**(i+1)
            parity=-1
        else:
            right += 2**(i+1)
            parity=1
#for (x,y) in interval:
#    print(x,y,y-x+1)
while digit >= 0:
    left,right = interval[digit]
    #print(N)
    if left <= N and N<= right:
        ansList.append('1')
        N -= parity*(2**digit)
    else:
        ansList.append('0')
    digit -= 1
    parity = -parity
ans = "".join(ansList)
print(ans)

Submission status: Accepted

Solution code:
import math


N = int(input())

if N == 0:
    print(0)
else:
    S = ""
    while N != 0:
        S = str(N + 2 * math.ceil(N / -2)) + S
        N = math.ceil(N / -2)

    print(S)

Submission status: Accepted

Solution code:
def convert_base(n):
    temp = n
    out = ''
    if n == 0:
        return '0'
    while temp != 0:
        q, r = divmod(temp, -2)
        if r == -1:
            q += 1
            r = 1
        out = str(r) + out
        temp = q
    return out

N = int(input())

print(convert_base(N))

Submission status: Accepted

Solution code:
import sys
N = int(sys.stdin.readline())

def div(a):
    return -(a // 2), a % 2


res = []
while N < 0 or 2 <= N:
    N, a = div(N)
    res.append(a)
res.append(N)

print(''.join(map(str, reversed(res))))

Submission status: Accepted

Solution code:
N = int(input())

if N == 0:
  print("0")
  exit()

res = ""
while N != 1:
  if N % 2 == 0:
    res += "0"
    N = N / (-2)
  else:
    res += "1"
    N = (N -1) / (-2)
res += "1"
print(res[::-1])

Submission status: Accepted

Solution code:
import math
N = int(input())

def retPlace(num):
    retVal = str(num % 2)
    num = math.ceil(num / (-2))
    if num != 0:
        retVal += retPlace(num)
    return retVal

ans = retPlace(N)
print(ans[::-1])

Submission status: Accepted

Solution code:
from collections import deque

def solve(n):
    queue = deque()
    while n != 0:
        r = -(n % (-2))
        #n = n // (-2) if r == 0 else (n - i) // (-2)
        n = (n - 1) // (-2)
        queue.appendleft(str(r))
    return 0 if len(queue) == 0 else "".join(queue)

if __name__ == "__main__":
    n = int(input())
    print(solve(n))

Submission status: Accepted

Solution code:
N = int(input())

res = ""

if N==0:print(0);exit()

while N!=0:
    tmp = N % -2
    N = N//-2 -tmp
    res = str(abs(tmp))+res

print(res)

Submission status: Accepted

Solution code:
def f(n):
    if n == 0:
        return "0"
    if n == 1:
        return "1"
    a = n // -2
    b = n % -2
    if b == -1:
        a += 1
        b += 2
    return f(a) + str(b)


def main():
    N = int(input())
    print(f(N))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N = int(input())

ans = []

if N == 0:
    print("0")
else:
    while N != 1:
        if N%2 == 0:
            ans.append("0")
            N = N//(-2)
        else:
            ans.append("1")
            N = (N-1)//(-2)
    else:
        ans.append("1")

print("".join(ans[::-1]))

Submission status: Accepted

Solution code:
import sys
import math
import collections
import itertools
import array
import inspect

# Set max recursion limit
sys.setrecursionlimit(10000)


# Debug output
def chkprint(*args):
    names = {
        id(v): k
        for k, v in inspect.currentframe().f_back.f_locals.items()
    }
    print(', '.join(
        names.get(id(arg), '???') + ' = ' + repr(arg) for arg in args))


# Binary converter
def to_bin(x):
    return bin(x)[2:]


def li_input():
    return [int(_) for _ in input().split()]


# --------------------------------------------

dp = None
c = []


def f(B):
    ans = 0
    for idx in range(len(B)):
        if B[len(B) - idx - 1] == "1":
            ans += ((-2)**idx)

    return ans


def is_lower(idx, c_value, N):
    add_value = 0
    for i in range(idx, -1, -1):
        if c[i] > 0:
            add_value += c[i]

    return c_value + add_value < N


# idx未満の添え字の数を引いて行った結果，
# 目的の数を上回ればidxを引く必要がある
def is_over(idx, c_value, N):
    sub_value = 0
    for i in range(idx, -1, -1):
        if c[i] < 0:
            sub_value += c[i]

    return c_value + sub_value > N


def main():
    N = int(input())
    c_value = None

    if N == 0:
        print(0)
        return

    for i in range(50):
        c.append((-2)**i)

    if N > 0:
        tmpsum = 0
        for i in range(50):
            if c[i] > 0:
                tmpsum += c[i]

            if tmpsum >= N:
                idx = i
                c_value = c[i]
                break

        ans = "1"

        for c_idx in range(idx - 1, -1, -1):
            if c[c_idx] < 0:
                if is_over(c_idx - 2, c_value, N):
                    ans += "1"
                    c_value += c[c_idx]
                else:
                    ans += "0"
            else:
                if is_lower(c_idx - 2, c_value, N):
                    ans += "1"
                    c_value += c[c_idx]
                else:
                    ans += "0"

    else:
        tmpsub = 0
        for i in range(50):
            if c[i] < 0:
                tmpsub += c[i]

            if tmpsub <= N:
                idx = i
                c_value = c[i]
                break

        ans = "1"

        for c_idx in range(idx - 1, -1, -1):
            if c[c_idx] < 0:
                if is_over(c_idx - 2, c_value, N):
                    ans += "1"
                    c_value += c[c_idx]
                else:
                    ans += "0"
            else:
                if is_lower(c_idx - 2, c_value, N):
                    ans += "1"
                    c_value += c[c_idx]
                else:
                    ans += "0"

    print(ans)


main()

Submission status: Accepted

Solution code:
N=int(input())
ans=[]

def calc(N):
    if N==0: return
    elif N>0:
        n=N
        power=-2
        while n>0:
            power+=2
            n-=2**power
    else:
        n=-N
        power=-1
        while n>0:
            power+=2
            n-=2**power
            
    ans.append(power)
   # print(power)
    calc(N-(-2)**power)

if N==0:print('0')
else:
    calc(N)
    answer=''
    for a in range(ans[0],-1,-1):
        if a in ans: answer=answer+'1'
        else: answer=answer+'0'
    print(answer)

Submission status: Accepted

Solution code:
round = lambda x: (x*2+1)//2

def rec(num):
    ans = round(num / (-2))
    remain = num - (-2)*ans
    if ans == 0:
        print(int(remain), end='')
        return
    rec(ans)
    print(int(remain), end='')
    return

def p_ans(num):
    rec(num)
    print()

if __name__ == '__main__':
    N = int(input())
    p_ans(N)

Submission status: Accepted

Solution code:
n = int(input())

if n == 0:
    print('0')
else:
    s = ''
    i = 0
    while n != 0:
        q = n % 2
        s = str(q) + s
        n = (n - q*((-1)**i)) // 2
        i += 1
    print(s)

Submission status: Accepted

Solution code:
n = int(input())
str = ''
if n == 0:
  str = '0'
else:
  while n != 0:
    if n % 2 == 0:
      str = '0' + str
      n /= (-2)
    else:
      str = '1' + str
      n -= 1
      n /= (-2)
print(str)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
N = int(input())
if N == 0:
  print('0')
  exit()

N_ = N
S = ''
b = 1

for i in range(100):
  N__ = abs(N_)
  if N__ % (b*2) == 0:
    S = '0' + S
  else:
    N_ = N_ - b
    S = '1' + S
  if N_ == 0:
    break
  b = b * (-2)

print(S)

Submission status: Accepted

Solution code:
def f(n):
    return (4 ** n - 1) // 3


n = int(raw_input())

if n < 0:
    m = 0
    while -2 * f(m) > n:
        m += 1
else:
    m = 0
    while f(m) < n:
        m += 1

if m == 0:
    print '0'
else:
    mask = sum(2 * 4 ** j for j in xrange(m))
    print bin((n + 2 * f(m)) ^ mask)[2:]

Submission status: Accepted

Solution code:
import numpy as np
import math

N = int(input())

# -10^9 <= N <= 10^9

k = 0

ret = ''

rest = N

while True:
    # print('ret=', ret)
    # print('rest=', rest)
    # print('rest%2', rest % 2)
    if rest % 2 == 1:
        ret += '1'
        if k % 2 == 0:
            rest -= 1
        else:
            rest += 1
    else:
        ret += '0'
    k += 1

    if rest == 0:
        break
    rest = int(rest / (2))
    

print(ret[::-1])

Submission status: Accepted

Solution code:
n = int(input())

if n != 0:
    ans = ''
    while n != 0:
        while n%2 == 0:
            n = n//(-2)
            ans = '0'+ans
        n = n-1
        ans ='1'+ans
        n = n/(-2)
else:
    ans = '0'
    
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
i = 0
s = ''
for _ in range(100):
    # print(n)
    d = n % 2**(i+1)
    # print(d)
    if d:
        n -= (-2)**i
        s = '1' + s
    else:
        s = '0' + s
    if n == 0:
        break
    i += 1
    
print(s)

Submission status: Accepted

Solution code:
def c_base_minus2_number(N):
    def negaternary(i):
        if i == 0:
            digits = ['0']
        else:
            digits = []
            while i != 0:
                i, remainder = divmod(i, -2)
                if remainder < 0:
                    i, remainder = i + 1, remainder + 2
                digits.append(str(remainder))
        return ''.join(digits[::-1])

    ans = negaternary(N)
    return ans

N = int(input())
print(c_base_minus2_number(N))