Cod sursa(job #3296175)

Utilizator christalknightChristian Micea christalknight Data 12 mai 2025 00:17:54
Problema Ridicare la putere in timp logaritmic Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.63 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) % 1999999973, (p - 1) / 2)) % 1999999973;
    else // p is even
        return exponentiate((n * n) % 1999999973, p / 2) % 1999999973;
} 

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

    fout<<exponentiate(n, p);

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