-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path372.super-pow.cpp
60 lines (58 loc) · 1.21 KB
/
372.super-pow.cpp
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
/*
* @lc app=leetcode id=372 lang=cpp
*
* [372] Super Pow
*
* https://leetcode.com/problems/super-pow/description/
*
* algorithms
* Medium (35.78%)
* Total Accepted: 28.9K
* Total Submissions: 80.6K
* Testcase Example: '2\n[3]'
*
* 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.
*
* Example 1:
*
*
*
* Input: a = 2, b = [3]
* Output: 8
*
*
*
* Example 2:
*
*
* Input: a = 2, b = [1,0]
* Output: 1024
*
*
*
*/
class Solution {
public:
int p;
int power(int a, int b){
if(b == 0) return 1;
if(b == 1) return a%p;
if(b%2==0){
int x = power(a,b/2);
return ((long long)x*(long long)x)%p;
}else{
int x = power(a, b-1);
return ((long long)x*(long long)a)%p;
}
}
int helper(int a, vector<int>& b, int idx){
if(idx == 0)
return power(a, b[idx]) % p;
return ( (long long)power(helper(a, b, idx-1), 10)%p * (long long)power(a, b[idx]) %p )%p;
}
int superPow(int a, vector<int>& b) {
p = 1337;
return helper(a, b, b.size()-1);
}
};