python solution beat 97.37%


  • 0
    H
    class Solution(object):
        def powMod(self,a, b):
            if not b:
                return 1
            phalf=self.powMod(a,b>>1)
            ans= (phalf*phalf) % 1337
            return (ans*a)%1337 if b&1 else ans
        def superPow(self, a, b):
            """
            :type a: int
            :type b: List[int]
            :rtype: int
            """
            a=a%1337
            if not b:
                return 1
            period=1
            m=(a*a)%1337
            while m!=a:
                period+=1
                m=(m*a)%1337
            v=0
            for d in b:
                v=(v*10+d)%period
            return self.powMod(a,v)
            
                    
                    
                    
                    
                    
    

Log in to reply
 

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