Cod sursa(job #3356715)

Utilizator Marius08Marius Benea Marius08 Data 3 iunie 2026 16:43:20
Problema Ridicare la putere in timp logaritmic Scor 100
Compilator c-64 Status done
Runda Arhiva educationala Marime 0.74 kb
#include <stdio.h>

long long exp_log(long long x, long long n) {
    /**
    if (n < 0) {
        x = 1./x;
        n = -n;
    }
        */
       x = x % 1999999973;

    if (n == 0)
        return 1;
    
    long long p = 1;
    while(n > 0) {
        if(n % 2 == 1) {
            p = (p * x) % 1999999973;
        }
        x = (x * x) % 1999999973;
        n /= 2;
    }
    return p;
}

int main() {
    FILE *fin = NULL;
    fin = fopen("lgput.in", "r");
    if(fin == NULL)
        return -1;
    long long n, p;
    fscanf(fin, "%lld %lld", &n, &p);

    FILE *fout = NULL;
    fout = fopen("lgput.out", "w");
    fprintf(fout, "%lld", exp_log(n, p));
    
    fclose(fin);
    fclose(fout);
    return 0;
}