Cod sursa(job #2917587)
| Utilizator | Data | 5 august 2022 20:15:12 | |
|---|---|---|---|
| Problema | Ridicare la putere in timp logaritmic | Scor | 10 |
| Compilator | cpp-64 | Status | done |
| Runda | Arhiva educationala | Marime | 0.4 kb |
#include <iostream>
#include <fstream>
std::ifstream fin("lgput.in");
std::ofstream fout("lgput.out");
int logarithmic(int n, int p)
{
if (p == 0)
{
return 1;
}
if (p % 2 == 1)
{
return n * logarithmic(n, p - 1);
}
if (p % 2 == 0)
{
n = logarithmic(n, p/2);
return n * n;
}
}
int main() {
int n, p;
fin >> n >> p;
fout << logarithmic(n, p);
return 0;
}