-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpowerInteger.cpp
More file actions
45 lines (37 loc) · 881 Bytes
/
powerInteger.cpp
File metadata and controls
45 lines (37 loc) · 881 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
45
/*
# Descrição:
A função "power" calcula a potência x^y (inteiros) sem problemas
de precisão. Isso é uma maneira alternativa para evitar problemas
com a função pow() do C++.
# Parâmetros:
ll x -> Base.
ll y -> Expoente (maior ou igual a zero).
# Retorno:
A função retorna o resultado de x^y (ambos long long int).
# Complexidade:
O(log(y)), onde y é o expoente.
# Referências:
https://www.geeksforgeeks.org/write-a-c-program-to-calculate-powxn/
# Problemas:
https://neps.academy/br/competition/1394/exercise/1312
*/
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
ll power(ll x, ll y)
{
ll temp;
if( y == 0)
return 1;
temp = power(x, y / 2);
if (y % 2 == 0)
return temp * temp;
else
return x * temp * temp;
}
int main()
{
ll resp = power(10, 18);
cout << resp << endl;
return 0;
}