• + 0 comments
    def solve(a, n, m):
        a = a % m
        pow10  = 10 ** len(str(a)) % m
        answer = 0
    
        while n:
            if n & 1:  #ie n is odd number
                answer = ((answer * pow10) % m + a) % m 
    
            a  = ((a * pow10) % m + a) % m
            pow10 = (pow10 * pow10) % m 
            n >>= 1 
    
        return answer
    

    https://www.rookieslab.com/posts/fast-power-algorithm-exponentiation-by-squaring-cpp-python-implementation