Cod sursa(job #681813)
| Utilizator | Data | 17 februarie 2012 20:37:50 | |
|---|---|---|---|
| Problema | Ridicare la putere in timp logaritmic | Scor | 10 |
| Compilator | c | Status | done |
| Runda | Arhiva educationala | Marime | 0.46 kb |
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
long long putere (int N, int P) {
if (P == 1)
return N;
if (P % 2 == 0) {
int x = putere(N, P/2);
return x * x;
}
else {
int x = putere(N, (P-1)/2);
return N * x * x;
}
}
int main () {
FILE *f_in = fopen("lgput.in", "r");
FILE *f_out = fopen("lgput.out", "w");
int N, P;
fscanf(f_in, "%d %d", &N, &P);
fprintf(f_out, "%lld", putere(N, P));
fclose(f_in);
fclose(f_out);
return 0;
}
