-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path#61.cpp
More file actions
44 lines (37 loc) · 735 Bytes
/
#61.cpp
File metadata and controls
44 lines (37 loc) · 735 Bytes
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
#include <iostream>
#include <string>
using namespace std;
int runtime = 0;
long long pow(int x, int y) {
long long result = 1;
int curr_pow = 0;
long long multiplier = x;
int mult_pow = 1;
while (curr_pow < y) {
if (result == 1) {
result *= multiplier;
curr_pow += mult_pow;
runtime++;
} else if (curr_pow + mult_pow <= y) {
curr_pow += mult_pow;
result *= multiplier;
multiplier *= multiplier;
mult_pow *= 2;
runtime++;
} else {
multiplier = x;
mult_pow = 1;
}
}
return result;
}
int main() {
string inp;
while (cin >> inp) {
int b = stoi(inp);
cin >> inp;
int e = stoi(inp);
cout << b << " ** " << e << " = " << pow(b, e) << endl << runtime << endl;
runtime = 0;
}
}