Problem p03104 (python)

Fixed submission:
def f(N):
    if N==0:
        return 0
    if N==1:
        return 1
    if N==2:
        return 1^2
    if N==3:
        return 0
    k = 2
    while 2**k<=N:
        k += 1
    k -= 1
    if 2**k==N:
        return N
    n = (N-2**k)//4
    cnt = 0
    i = 2**k+4*n
    while i<=N:
        cnt = cnt^i
        i += 1
    return cnt
A,B = map(int,input().split())
if A==0 and B==0:
    print(0)
elif A==0 and B>0:
    print(f(B))
else:
    print(f(B)^f(A-1))

Buggy submission (verdict: Runtime Error):
A,B = map(int,input().split())
n=B-A+1
a = "0"*(50-len(bin(A))+2)+bin(A)[2:]
b = ""
for k in range(1,51):
    if k==1:
        x = str(r%2)
    else:
        m = int(a[-k:],2)
        r = n%(2**k)
        if a[-k]=="0":
            if (m+r-1)%(2**k)>=2**(k-1):
                x = str((m+r-1-2**(k-1)+1)%2)
            else:
                x = "0"
        else:
            if (m+r-1)%(2**k)>=2**(k-1):
                x = str(r%2)
            else:
                x = str((2**k-1-m+1)%2)
    b = x+b
print(int(b,2))

Buggy submission (verdict: Runtime Error):
A,B = map(int,input().split())
k = len(bin(B))-2
if k==1:
    totB = bin(B)[-1]
elif B==2**k-1:
    totB = "0"*k
else:
    if bin(B)[-2:]=="00":
        totB = "00"
    elif bin(B)[-2:]=="01":
        totB = "01"
    elif bin(B)[-2:]=="10":
        totB = "11"
    elif bin(B)[-2:]=="11":
        totB = "00"
    a = bin(B)[-1]
    for i in range(-3,-k-1,-1):
        if bin(B)[i]=="0":
            totB = "0"+totB
        elif bin(B)[i]=="1" and a=="1":
            totB = "0"+totB
        elif bin(B)[i]=="1" and a=="0":
            totB = "1"+totB
if A==0:
    totA == "0"
else:
    m = len(bin(A-1))-2
    if m==1:
        totA = bin(A-1)[-1]
    elif A-1==2**m-1:
        totA = "0"*m
    else:
        if bin(A-1)[-2:]=="00":
            totA = "00"
        elif bin(A-1)[-2:]=="01":
            totA = "01"
        elif bin(A-1)[-2:]=="10":
            totA = "11"
        elif bin(A-1)[-2:]=="11":
            totA = "00"
        a = bin(A-1)[-1]
        for i in range(-3,-m-1,-1):
            if bin(A-1)[i]=="0":
                totA = "0"+totA
            elif bin(A-1)[i]=="1" and a=="1":
                totA = "0"+totA
            elif bin(A-1)[i]=="1" and a=="0":
                totA = "1"+totA
totA = int(totA,2)
totB = int(totB,2)
tot = totA^totB
print(tot)

Buggy submission (verdict: Runtime Error):
A,B = map(int,input().split())
k = len(bin(B))-2
if k==1:
    totB = bin(B)[-1]
elif B==2**k-1:
    totB = "0"*k
else:
    if bin(B)[-2:]=="00":
        totB = "00"
    elif bin(B)[-2:]=="01":
        totB = "01"
    elif bin(B)[-2:]=="10":
        totB = "11"
    elif bin(B)[-2:]=="11":
        totB = "00"
    a = bin(B)[-1]
    for i in range(-3,-k-1,-1):
        if bin(B)[i]=="0":
            totB = "0"+totB
        elif bin(B)[i]=="1" and a=="1":
            totB = "0"+totB
        elif bin(B)[i]=="1" and a=="0":
            totB = "1"+totB
# print("totB={}".format(totB))
m = len(bin(A-1))-2
if m==1:
    totA = bin(A-1)[-1]
elif A-1==2**m-1:
    totA = "0"*m
else:
    if bin(A-1)[-2:]=="00":
        totA = "00"
    elif bin(A-1)[-2:]=="01":
        totA = "01"
    elif bin(A-1)[-2:]=="10":
        totA = "11"
    elif bin(A-1)[-2:]=="11":
        totA = "00"
    a = bin(A-1)[-1]
    for i in range(-3,-m-1,-1):
        if bin(A-1)[i]=="0":
            totA = "0"+totA
        elif bin(A-1)[i]=="1" and a=="1":
            totA = "0"+totA
        elif bin(A-1)[i]=="1" and a=="0":
            totA = "1"+totA
totA = int(totA,2)
totB = int(totB,2)
tot = totA^totB
print(tot)