Cod sursa(job #3296173)

Utilizator christalknightChristian Micea christalknight Data 12 mai 2025 00:15:24
Problema Ridicare la putere in timp logaritmic Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.58 kb
//https://infoarena.ro/problema/lgput

#include <iostream>
#include <fstream>

using namespace std;

ifstream fin("lgput.in");
ofstream fout("lgput.out");

unsigned exponentiate(unsigned n, unsigned p){ //computes and returns n to the power of p (modulo 1999999973)
    if (p == 0)
        return 1;

    if (p % 2) // p is odd
        return n * exponentiate(n * n, (p - 1) / 2);
    else // p is even
        return exponentiate(n * n, p / 2);
} 

int main(){
    unsigned n, p;
    fin>>n>>p;

    fout<<exponentiate(n, p);

    fin.close();
    fout.close();
}