Skip to content

Commit 95445ac

Browse files
committed
372
1 parent 2fb60c4 commit 95445ac

File tree

5 files changed

+90
-1
lines changed

5 files changed

+90
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ Leetcode solutions in C++, Java, Python
33

44
|#|Title|Solution|Difficulty|
55
|---|-----|--------|----------|
6+
|372|[Super Pow](./src/super_pow)|[C++](./src/super_pow)|Medium|
67
|371|[Sum of Two Integers](./src/sum_of_two_integers)|[C++](./src/sum_of_two_integers)|Easy|
78
|370|[Range Addition](./src/range_addition)|[C++](./src/range_addition)|Medium|
89
|369|[Plus One Linked List](./src/plus_one_linked_list)|[C++](./src/plus_one_linked_list)|Medium|

src/sum_of_two_integers/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Problem
22
===
3-
late the sum of two integers a and b, but you are not allowed to use the operator + and -.
3+
calculate the sum of two integers a and b, but you are not allowed to use the operator + and -.
44

55
Example:
66
Given a = 1 and b = 2, return 3.

src/super_pow/Makefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
all:
2+
clang++ main.cpp -o main.o -std=c++11
3+
4+
run:
5+
clang++ main.cpp -o main.o -std=c++11
6+
./main.o

src/super_pow/README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
Problem
2+
===
3+
4+
Your task is to calculate a ** b mod 1337 where a is a positive integer and b is an extremely large positive integer given in the form of an array.
5+
6+
Example1:
7+
8+
a = 2
9+
b = [3]
10+
11+
Result: 8
12+
Example2:
13+
14+
a = 2
15+
b = [1,0]
16+
17+
Result: 1024

src/super_pow/main.cpp

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#include <algorithm>
2+
#include <cstdlib>
3+
#include <iostream>
4+
#include <map>
5+
#include <queue>
6+
#include <set>
7+
#include <sstream>
8+
#include <stack>
9+
#include <string>
10+
#include <unordered_map>
11+
#include <unordered_set>
12+
#include <vector>
13+
14+
using namespace std;
15+
16+
class Solution {
17+
private:
18+
int mod = 1337;
19+
public:
20+
int superPow(int a, vector<int>& b) {
21+
int n = b.size();
22+
int result = 1;
23+
for (int i = n - 1; i >= 0; i--) {
24+
result = result * mod_pow(a, b[i]) % mod;
25+
a = mod_pow(a, 10);
26+
}
27+
return result;
28+
}
29+
inline int mod_pow(int a, int b) {
30+
int ans = 1;
31+
a %= mod;
32+
while (b > 0) {
33+
if (b & 1) {
34+
ans = ans * a % mod;
35+
}
36+
a = a * a % mod;
37+
b >>= 1;
38+
}
39+
return ans;
40+
}
41+
};
42+
43+
int main() {
44+
Solution sol;
45+
int a;
46+
vector<int> b;
47+
48+
a = 2;
49+
b = {3};
50+
cout << sol.superPow(a, b) << endl;
51+
52+
a = 2;
53+
b = {1, 0};
54+
cout << sol.superPow(a, b) << endl;
55+
56+
a = 647;
57+
b = {2, 0, 0};
58+
cout << sol.superPow(a, b) << endl;
59+
60+
a = 2147483647;
61+
b = {2, 0, 0};
62+
cout << sol.superPow(a, b) << endl;
63+
64+
return 0;
65+
}

0 commit comments

Comments
 (0)