C++ Concise Iterative Solution


  • 0
    Z
    class Solution {
    public:
    	int superPow(int a, vector<int>& b) {
    		a %= 1337;
    		int res = 1, len = b.size();
    		for (int i = b.back(); i != 0; --i) res = (res * a) % 1337;
    		for (int i = len - 2, tmp, k; i >= 0; --i) {
    			for (k = 10, tmp = 1; k != 0; --k) tmp = (tmp * a) % 1337;
    			a = tmp;
    			for (k = b[i], tmp = 1; k != 0; --k) tmp = (tmp * a) % 1337;
    			res = (res * tmp) % 1337;
    		}
    		return res;
    	}
    };
    

Log in to reply
 

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