python recurisive, with example easy to understand


  • 0
    B

    ex: a = 3, b = 11 ,ret = 1 * a^b
    a^b = 3 ^11 = 3^10 ^3 = 3^(5 * 2) * 3 = (3^2)^5*3
    ==> ret = 3 , a= 3^2= 9,b = 5, ret = 1 * 3 * a^b
    a^b = 9^5 = 9^4 * 9.....

        def superPow(self, a, b):
            """
            :type a: int
            :type b: List[int]
            :rtype: int
            """
            a = a%1337
            b = int(''.join([str(i) for i in b]))
            re = 1
            while b!= 0:
                if b&1 == 1:
                    re = (re*a)%1337
                a = (a*a)%1337
                b = b>>1
            return re
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.