Cod sursa(job #1129373)
| Utilizator | Data | 27 februarie 2014 21:44:45 | |
|---|---|---|---|
| Problema | Ridicare la putere in timp logaritmic | Scor | 10 |
| Compilator | cpp | Status | done |
| Runda | Arhiva educationala | Marime | 0.46 kb |
#include <iostream>
#include <fstream>
using namespace std;
long long Putere(int x, int y)
{
if (y == 0)
return 1;
else if (y == 1)
return x;
else if (y % 2 == 0)
return Putere(x * x, y / 2);
else
return x * Putere(x * x, (y - 1) / 2);
}
int main()
{
ifstream f("lgput.in");
int a, b;
f >> a >> b;
f.close();
ofstream g("lgput.out");
g << Putere(a, b);
g.close();
return 0;
}
