Cod sursa(job #3296176)

Utilizator christalknightChristian Micea christalknight Data 12 mai 2025 00:19:21
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;

#define MOD 1999999973

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) % MOD, (p - 1) / 2) % MOD;
    else // p is even
        return exponentiate((n * n) % MOD, p / 2) % MOD;
} 

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

    fout<<exponentiate(n, p);

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