Non-Recursive Method for C++ Clean and Short Solution


  • 0

    Non-Recursive version for C++ Clean and Short Solution with quick exp method.

    class Solution {
    public:
    
        int MOD = 1337;
        
        int superPow(int a, vector<int>& b) {
            int res = 1;
            for (int i = 0; i < b.size(); i++) 
                res = (quick_exp(res, 10) * quick_exp(a, b[i])) % MOD;
            return res;
        }
        
        int quick_exp(int a, int b) {
            a %= MOD;
            int prod = 1;
            while (b) {
                if (b&1) {
                    prod = (prod * a) % MOD;
                    b -= 1;
                } else {
                    a = (a*a) % MOD;
                    b >>= 1;
                }
            }
            return prod;
        }
    };
    

Log in to reply
 

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