Problem p03286 (python)

Fixed submission:
# -*- coding: utf-8 -*-
import logging
import time


class Solver:
    def __init__(self):
        self.n = 0

    # 入力
    def input_value(self):
        # self.n = int(input())
        # self.A, self.B = map(int, input().split())
        # self.A = list(map(int, input().split()))
        # self.a, self.b, self.c = map(int, [input() for i in range(4)])
        # self.t = [None] * self.n
        self.n = int(input())

    @staticmethod
    def sigma(n):
        x = 0
        if n % 2 == 0:
            for i in range(0, n + 1, 2):
                x += (-2) ** i
        else:
            for i in range(1, n + 1, 2):
                x += (-2) ** i
        return x

    @staticmethod
    def to_base_m2(n):
        # logging.debug(input_str)
        if n == 0:
            return '0'

        MAX_BIT = 35
        s = ''
        i = MAX_BIT
        while i >= 0:
            if i % 2 == 0:
                if Solver.sigma(i - 2) < n <= Solver.sigma(i):
                    s = s + '1'
                    n -= (-2) ** i
                else:
                    s = s + '0'
            elif i % 2 == 1:
                if Solver.sigma(i) <= n < Solver.sigma(i - 2):
                    s = s + '1'
                    n -= (-2) ** i
                else:
                    s = s + '0'
            i -= 1
        while s[0] == '0':
            s = s[1:]
        return s

        # logging.debug(input_str)



    # def run(self):
    #     if self.n % self.k == 0:
    #         return 0
    #     return 1

    @staticmethod
    def test():
        start_time = time.time()

        assert Solver.sigma(2) == 5, 'Error.'
        assert Solver.sigma(3) == -10, 'Error.'
        assert Solver.to_base_m2(9) == '11001', 'Error.'
        assert Solver.to_base_m2(-9) == '1011', 'Error.'
        assert Solver.to_base_m2(123456789) == '11000101011001101110100010101', 'Error.'
        assert Solver.to_base_m2(0) == '0', 'Error.'
        assert Solver.to_base_m2(1) == '1', 'Error.'
        assert Solver.to_base_m2(2) == '110', 'Error.'
        assert Solver.to_base_m2(-1) == '11', 'Error.'
        assert Solver.to_base_m2(-2) == '10', 'Error.'

        elapsed_time = time.time() - start_time
        logging.debug('elapsed_time: {0}'.format(elapsed_time) + '[sec]')


if __name__ == '__main__':
    logging.basicConfig(level=logging.DEBUG)

    solver = Solver()

    # Solver.test()

    solver.input_value()
    ans = solver.to_base_m2(solver.n)
    # print(str(ans))
    print(ans)

Buggy submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
import logging
import time


class Solver:
    def __init__(self):
        self.n = 0

    # 入力
    def input_value(self):
        # self.n = int(input())
        # self.A, self.B = map(int, input().split())
        # self.A = list(map(int, input().split()))
        # self.a, self.b, self.c = map(int, [input() for i in range(4)])
        # self.t = [None] * self.n
        self.n = int(input())

    @staticmethod
    def sigma(n):
        x = 0
        if n % 2 == 0:
            for i in range(0, n + 1, 2):
                x += (-2) ** i
        else:
            for i in range(1, n + 1, 2):
                x += (-2) ** i
        return x

    @staticmethod
    def to_base_m2(n):
        # logging.debug(input_str)
        if n == 0:
            return '0'

        MAX_BIT = 30
        s = ''
        i = MAX_BIT
        while i >= 0:
            if i % 2 == 0:
                if Solver.sigma(i - 2) < n <= Solver.sigma(i):
                    s = s + '1'
                    n -= (-2) ** i
                else:
                    s = s + '0'
            elif i % 2 == 1:
                if Solver.sigma(i) <= n < Solver.sigma(i - 2):
                    s = s + '1'
                    n -= (-2) ** i
                else:
                    s = s + '0'
            i -= 1
        while s[0] == '0':
            s = s[1:]
        return s

        # logging.debug(input_str)



    # def run(self):
    #     if self.n % self.k == 0:
    #         return 0
    #     return 1

    @staticmethod
    def test():
        start_time = time.time()

        assert Solver.sigma(2) == 5, 'Error.'
        assert Solver.sigma(3) == -10, 'Error.'
        assert Solver.to_base_m2(9) == '11001', 'Error.'
        assert Solver.to_base_m2(-9) == '1011', 'Error.'
        assert Solver.to_base_m2(123456789) == '11000101011001101110100010101', 'Error.'
        assert Solver.to_base_m2(0) == '0', 'Error.'
        assert Solver.to_base_m2(1) == '1', 'Error.'
        assert Solver.to_base_m2(2) == '110', 'Error.'
        assert Solver.to_base_m2(-1) == '11', 'Error.'
        assert Solver.to_base_m2(-2) == '10', 'Error.'

        elapsed_time = time.time() - start_time
        logging.debug('elapsed_time: {0}'.format(elapsed_time) + '[sec]')


if __name__ == '__main__':
    logging.basicConfig(level=logging.DEBUG)

    solver = Solver()

    # Solver.test()

    solver.input_value()
    ans = solver.to_base_m2(solver.n)
    # print(str(ans))
    print(ans)