-
Notifications
You must be signed in to change notification settings - Fork 0
/
372.hpp
60 lines (46 loc) · 1.01 KB
/
372.hpp
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#ifndef LEETCODE_372_HPP
#define LEETCODE_372_HPP
#include <iostream>
#include <queue>
#include <algorithm>
#include <vector>
#include <unordered_map>
#include <unordered_set>
#include <set>
#include <numeric>
#include <stack>
#include <string>
using namespace std;
/*
* 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.
Example1:
a = 2
b = [3]
Result: 8
Example2:
a = 2
b = [1,0]
Result: 1024
*/
class Solution {
public:
int superPow(int a, vector<int> &b) {
if (b.empty())
return 1;
int last = b.back();
b.pop_back();
return powmod(superPow(a, b), 10) * powmod(a, last) % base;
}
private:
const int base = 1337;
int powmod(int a, int k) { // a^k % 1337 0<= k <= 10
a %= base;
int result = 1;
for (int i = 0; i < k; ++i) {
result = (result * a) % base;
}
return result;
}
};
#endif //LEETCODE_372_HPP