372. Super Pow
Your task is to calculate ab mod 1337 where a is a positive integer and b is an extremely large positive integer given in the form of an array.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| class Solution { const int mod = 1337; bool isZero(vector<int>& b) { for(auto& num : b) if(num) return true; return false; } void div(vector<int>& b) { bool remainder = false; for(int i = 0; i < b.size(); i++) { int prev = b[i]; b[i] >>= 1; if(remainder) b[i] += 5; remainder = prev & 1; } } public: int superPow(long a, vector<int>& b) { long res = 1; while(isZero(b)) { if(b.back() & 1) res = (res * a) % mod; a = (a * a) % mod; div(b); } return res; } };
|