1: class Solution {
2: private:
3: const int k = 1337;
4: int powmod(int a, int b) {
5: // (a^b)%k = (a^(b-1)*a)%k = (a^(b-1)%k)*(a%k)%k
6: int res = 1;
7: a %= k;
8: for (int i = 0; i < b; i++) {
9: res = res * a % k;
10: }
11: return res;
12: }
13: public:
14: int superPow(int a, vector<int>& b) {
15: // ab % k = (a%k)(b%k)%k
16: // a^123 % k = (a^120*a^3) % k = (a^120%k)(a^3%k)%k
17: if (b.empty()) return 1;
18: int last = b.back();
19: b.pop_back();
20: return powmod(superPow(a, b), 10) * powmod(a, last) % k;
21: }
22: };
Wednesday, August 17, 2016
372. Super Pow
I don’t have any clue. This is completely a math problem. I have to follow the top rated solution.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment