Cod sursa(job #1291925)
Utilizator | Data | 13 decembrie 2014 15:07:59 | |
---|---|---|---|
Problema | Ridicare la putere in timp logaritmic | Scor | 10 |
Compilator | cpp | Status | done |
Runda | Arhiva educationala | Marime | 0.41 kb |
#include <iostream>
#include <fstream>
using namespace std;
ifstream f ("lgput.in");
ofstream g ("lgput.out");
double pow_log(int x, int n)
{
double p = 1 ;
while (n > 0)
{
if (n & 1) // n este impar
{
p *= x;
n-- ;
}
x = x * x ;
n >>= 1 ; // sau n = n / 2
}
return p ;
}
int main()
{
int a, b;
f >> a >> b;
g << pow_log(a, b);
return 0;
}