Pagini recente » Cod sursa (job #2800616) | Cod sursa (job #621769) | Cod sursa (job #814176) | Cod sursa (job #2353698) | Cod sursa (job #2646802)
#include <fstream>
using namespace std;
int exp_by_squaring(int x, int exp);
int main() {
ifstream fin("lgput.in");
ofstream fout("lgput.out");
int x, exp;
fin >> x >> exp;
fout << exp_by_squaring(x, exp);
return 0;
}
int exp_by_squaring(int x, int exp) {
if (exp < 0) return exp_by_squaring(1 / x, -exp);
if (exp == 0) return 1;
if (exp == 1) return x;
if (exp % 2 == 0) {
return exp_by_squaring(x * x, exp / 2);
} else {
return exp_by_squaring(x * x, (exp - 1) / 2);
}
}