Cod sursa(job #2931812)
Utilizator | Data | 31 octombrie 2022 23:07:29 | |
---|---|---|---|
Problema | Ridicare la putere in timp logaritmic | Scor | 10 |
Compilator | cpp-64 | Status | done |
Runda | Arhiva educationala | Marime | 0.43 kb |
#include <iostream>
#include <fstream>
using namespace std;
ifstream fin("lgput.in");
ofstream fout("lgput.out");
int logh_exp(int N,int P){
if(P<0)
return logh_exp(1/N,-P);
if(P == 0)
return 1;
if(P % 2 == 0 ){
return logh_exp(N*N,P/2);
}
else
return N*logh_exp(N*N,(P-1)/2);
}
int main()
{
int N,P;
fin>>N>>P;
fout<<logh_exp(N,P);
return 0;
}