Cod sursa(job #3227186)
| Utilizator | Data | 26 aprilie 2024 21:23:26 | |
|---|---|---|---|
| Problema | Ridicare la putere in timp logaritmic | Scor | 10 |
| Compilator | c-64 | Status | done |
| Runda | Arhiva educationala | Marime | 0.56 kb |
#include <stdio.h>
long fast_exp(int x, int n) {
if(n < 0) {
x = 1 / x;
n = -n;
}
if(n == 0) return 1;
int y = 1;
while(n > 1) {
if(n % 2 != 0) {
y = x * y;
n--;
}
x *= x;
n /= 2;
}
return x * y;
}
int main() {
long N, P;
FILE *in = fopen("lgput.in", "r");
FILE *out = fopen("lgput.out", "w");
fscanf(in, "%ld %ld", &N, &P);
fclose(in);
fprintf(out, "%ld", fast_exp(N, P));
fclose(out);
return 0;
}