Cod sursa(job #2931824)

Utilizator ClotanPClotan Paul Ioan ClotanP Data 31 octombrie 2022 23:20:53
Problema Ridicare la putere in timp logaritmic Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.49 kb
#include <iostream>
#include <fstream>

#define Nr 1999999973
using namespace std;
ifstream fin("lgput.in");
ofstream fout("lgput.out");

long long logh_exp(long long N,long long P){
    if(P<0)
        return logh_exp(1/N,-P);
    if(P == 0)
        return 1;
    if(P % 2 == 0 ){
        return logh_exp((N*N)%Nr,P/2);
    }
    else
        return N*logh_exp((N*N)%Nr,(P-1)/2);
}

int main()
{
    long long  N,P;
    fin>>N>>P;
    fout<<logh_exp(N,P);

    return 0;
}