Short c++ solution O(n). No recursion. Does not require mathmatics knowledge.


  • 0
    X
        int superPow(int a, vector<int>& b) {
            int result = 1;
            a %= 1337;
            for(int i = 0;i<b.size();i++){
                for(int j = 0;j<b[b.size()-i-1];j++){
                    result=(result*a)%1337;
                }
                int a2 = (a*a)%1337;
                int a4 = (a2*a2)%1337;
                int a8 = (a4*a4)%1337; 
                a = (a8*a2)%1337;
            }
            return result;
        }
    

Log in to reply
 

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