Problem p03104 (python)

Fixed submission:
#### import ####
import sys
import math
from collections import defaultdict

#### 設定 ####
sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

#### 定数 ####
mod = 10**9 + 7

#### 読み込み ####
def I(): return int(input())
def II(): return map(int, input().split())
def III(): return list(map(int, input().split()))
def Line(N):
    read_all = [tuple(map(int, input().split())) for _ in range(N)]
    return map(list,zip(*read_all))

#################

A,B = II()

b = format(B,'b')
n = len(b)
a = format(A,'b').zfill(n)
num = B-A+1

ans = str()
for i in range(n):
    if i==0:
        q,m = divmod(num,2)
        if q%2==0:
            if m==1:
                ans += a[n-i-1]
            else:
                ans += '0'
        else:
            if m==1:
                if a[n-i-1]=='0':
                    ans += '1'
                else:
                    ans += '0'
            else:
                ans += '1'
        continue       
    if a[n-i-1]=='0':
        temp = (1<<i)-int(a[-i:],2)
        if temp >= num:
            ans += '0'
        else:
            val = num-temp
            q,m = divmod(val,2**i)
            if q%2==1:
                ans += '0'
            else:
                if m%2==1:
                    ans += '1'
                else:
                    ans += '0'
    else:
        temp = (1<<(i+1))-int(a[-(i+1):],2)
        if temp >= num:
            if num%2==0:
                ans += '0'
            else:
                ans += '1'
        else:
            val = num-temp
            q,m = divmod(val,2**i)
            if q%2==1:
                if m%2==1:
                    if temp%2==0:
                        ans += '1'
                    else:
                        ans += '0'
                else:
                    if temp%2==0:
                        ans += '0'
                    else:
                        ans += '1'
            else:
                if temp%2==0:
                    ans += '0'
                else:
                    ans += '1'

ans = ans[::-1]
print(int(ans,2))

Buggy submission (verdict: Runtime Error):
#### import ####
import sys
import math
from collections import defaultdict

#### 設定 ####
sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

#### 定数 ####
mod = 10**9 + 7

#### 読み込み ####
def I(): return int(input())
def II(): return map(int, input().split())
def III(): return list(map(int, input().split()))
def Line(N):
    read_all = [tuple(map(int, input().split())) for _ in range(N)]
    return map(list,zip(*read_all))

#################

A,B = II()

b = format(B,'b')
n = len(b)
a = format(A,'b').zfill(n)
num = B-A+1

ans = str()
for i in range(n):
    if i==0:
        q,m = divmod(num,2)
        if q%2==0:
            if m==1:
                ans += a[n-i-1]
            else:
                ans += 0
        else:
            if m==0:
                ans += a[n-i-1]
            else:
                ans += 0
        continue       
    if a[n-i-1]=='0':
        temp = (1<<i)-int(a[-i:],2)
        if temp >= num:
            ans += '0'
        else:
            val = num-temp
            q,m = divmod(val,2**i)
            if q%2==1:
                ans += '0'
            else:
                if m%2==1:
                    ans += '1'
                else:
                    ans += '0'
    else:
        temp = (1<<(i+1))-int(a[-(i+1):],2)
        if temp >= num:
            if num%2==0:
                ans += '0'
            else:
                ans += '1'
        else:
            val = num-temp
            q,m = divmod(val,2**i)
            if q%2==1:
                if m%2==1:
                    if temp%2==0:
                        ans += '1'
                    else:
                        ans += '0'
                else:
                    if temp%2==0:
                        ans += '0'
                    else:
                        ans += '1'
            else:
                if temp%2==0:
                    ans += '0'
                else:
                    ans += '1'

ans = ans[::-1]
print(int(ans,2))

Buggy submission (verdict: Runtime Error):
#### import ####
import sys
import math
from collections import defaultdict

#### 設定 ####
sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

#### 定数 ####
mod = 10**9 + 7

#### 読み込み ####
def I(): return int(input())
def II(): return map(int, input().split())
def III(): return list(map(int, input().split()))
def Line(N):
    read_all = [tuple(map(int, input().split())) for _ in range(N)]
    return map(list,zip(*read_all))

#################

A,B = II()

b = format(B,'b')
n = len(b)
a = format(A,'b').zfill(n)
num = B-A+1

ans = str()
for i in range(n):
    if i==0:
        q,m = divmod(num,2)
        if q%2==0:
            if m==1:
                ans += a[n-i-1]
            else:
                ans += 0
        else:
            if m==0:
                ans += a[n-i-1]
            else:
                ans += 0
        continue       
    if a[n-i-1]=='0':
        temp = (1<<i)-int(a[-i:],2)
        if temp >= num:
            ans += '0'
        else:
            val = num-temp
            q,m = divmod(val,2**i)
            if q%2==1:
                ans += '0'
            else:
                if m%2==1:
                    ans += '1'
                else:
                    ans += '0'
    else:
        temp = (1<<(i+1))-int(a[-(i+1):],2)
        if temp >= num:
            if num%2==0:
                ans += '0'
            else:
                ans += '1'
        else:
            val = num-temp
            q,m = divmod(val,2**i)
            if q%2==1:
                if m%2==1:
                    ans += '0'
                else:
                    ans += '1'
            else:
                ans += '1'

ans = ans[::-1]
print(int(ans,2))