Cod sursa(job #3227185)

Utilizator thek0derHorja Razvan thek0der Data 26 aprilie 2024 21:21:20
Problema Ridicare la putere in timp logaritmic Scor 0
Compilator c-64 Status done
Runda Arhiva educationala Marime 0.59 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("1gput.in", "r");
    FILE *out = fopen("1gput.out", "w");

    fscanf(in, "%ld %ld", &N, &P);
    printf("%ld %ld", N, P);
    fclose(in);

    fprintf(out, "%ld", fast_exp(N, P));
    fclose(out);

    return 0;
}