def my_pow(b,e,m):
    """ Computes b^e mod m using the square and multiply algorithm"""
    if e == 0:
        print('exponent is zero')
        return 1
    ## enter your source code here
    
    r = 1
    bn = bin(e)[2:]     # convert the exponent to binary format, discard the prefix ‘0b’
    i = 0
    while (i < len(bn)):
        r = (r * r) % m     # square is done automatically for every exponent digit
        if int(bn[i]) != 0:     # multiply is done only if the exponent is 1
            r = (r * b) % m
        i = i + 1    
    return r