Cod sursa(job #2246271)

Utilizator dia.ionescuIonescu Diana dia.ionescu Data 26 septembrie 2018 21:26:31
Problema Ridicare la putere in timp logaritmic Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.5 kb
#include <iostream>
#include <fstream>

using namespace std;

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

#define DIV 1999999973

long long lgput(long long n, long long p){
    if (p == 0)
        return 1;
    if (p == 1)
        return n % DIV;
    if (p % 2 == 0)
        return lgput((n * n) % DIV, p / 2);
    if  (p % 2 == 1)
        return n * lgput((n * n) % DIV, (p - 1) / 2) % DIV;
}
int main()
{
    long long n, p;
    fin >> n >> p;
    fout << lgput(n, p);
}