Problem p03138 (python)

Fixed submission:
L = 50

def get_binary_str(n: int) -> str:
    binary = bin(n)[2:]
    padding_length = L - len(binary)
    return '0' * padding_length + binary

def get_first_one_pos(s: str) -> int:
    i = 0
    while i < len(s) and s[i] == '0':
        i += 1
    return i

def xor(x: int, y: int) -> int:
    if x == y:
        return 0
    else:
        return 1

def calc_xor(i: int, x: int, binary_As) -> int:
    ret = 0
    for binary_A in binary_As:
        ret += xor(x, int(binary_A[i]))
    return ret


N, K = list(map(int, input().split(' ')))
As = list(map(int, input().split(' ')))

binary_As = [get_binary_str(A) for A in As]
binary_K = get_binary_str(K)
one_pos = get_first_one_pos(binary_K)

i = 0
ret = 0
while i < one_pos:
    ret += calc_xor(i, 0, binary_As) * 2**(L-i-1)
    i += 1

flag = False
while i < L:
    a = calc_xor(i, 0, binary_As)
    b = N - a
    if flag:
        ret += a * 2**(L-i-1) if a > b else b * 2**(L-i-1)
    else:
        if binary_K[i] == '1':
            if a > b:
                flag = True
            ret += a * 2**(L-i-1) if a > b else b * 2**(L-i-1)
        else:
            ret += a * 2**(L-i-1)

    i += 1

print(ret)

Buggy submission (verdict: Runtime Error):
from typing import List


L = 40

def get_binary_str(n: int) -> str:
    binary = bin(n)[2:]
    padding_length = L - len(binary)
    return '0' * padding_length + binary

def get_first_one_pos(s: str) -> int:
    i = 0
    while i < len(s) and s[i] == '0':
        i += 1
    return i

def xor(x: int, y: int) -> int:
    if x == y:
        return 0
    else:
        return 1

def calc_xor(i: int, x: int, binary_As: List[List[str]]) -> int:
    ret = 0
    for binary_A in binary_As:
        ret += xor(x, int(binary_A[i]))
    return ret


N, K = list(map(int, input().split(' ')))
As = list(map(int, input().split(' ')))

binary_As = [get_binary_str(A) for A in As]
binary_K = get_binary_str(K)
one_pos = get_first_one_pos(binary_K)

i = 0
ret = 0
while i < one_pos:
    ret += calc_xor(i, 0, binary_As) * 2**(L-i-1)
    i += 1

while i < L:
    a = calc_xor(i, 0, binary_As)
    b = calc_xor(i, 1, binary_As)
    ret += a * 2**(L-i-1) if a > b else b * 2**(L-i-1)
    i += 1

print(ret)

Buggy submission (verdict: Runtime Error):
from typing import List


L = 40

def get_binary_str(n: int) -> str:
    binary = bin(n)[2:]
    padding_length = L - len(binary)
    return '0' * padding_length + binary

def get_first_one_pos(s: str) -> int:
    i = 0
    while i < len(s) and s[i] == '0':
        i += 1
    return i

def xor(x: int, y: int) -> int:
    if x == y:
        return 0
    else:
        return 1

def calc_xor(i: int, x: int, binary_As: List[List[str]]) -> int:
    ret = 0
    for binary_A in binary_As:
        ret += xor(x, int(binary_A[i]))
    return ret


def main():
    N, K = list(map(int, input().split(' ')))
    As = list(map(int, input().split(' ')))

    binary_As = [get_binary_str(A) for A in As]
    binary_K = get_binary_str(K)
    one_pos = get_first_one_pos(binary_K)

    i = 0
    ret = 0
    while i < one_pos:
        ret += calc_xor(i, 0, binary_As) * 2**(L-i-1)
        i += 1

    while i < L:
        a = calc_xor(i, 0, binary_As)
        b = calc_xor(i, 1, binary_As)
        ret += a * 2**(L-i-1) if a > b else b * 2**(L-i-1)
        i += 1

    print(ret)

if __name__ == '__main__':
    main()