Cod sursa(job #2417349)

Utilizator AlexutAlex Calinescu Alexut Data 29 aprilie 2019 16:08:16
Problema Ridicare la putere in timp logaritmic Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.51 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;
    return 0;
}
int main()
{
    long long n, p;
    fin >> n >> p;
    fout << lgput(n, p);
}