Cod sursa(job #2755870)

Utilizator RedPipperNastasa Stefan-Alexandru RedPipper Data 28 mai 2021 16:54:03
Problema Ridicare la putere in timp logaritmic Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.43 kb
#include <fstream>

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


long long n, p;

long long exp(long long x, long long n){

    if(n < 0)return exp(1/x, -n);
    if(n==0)return 1;
    if(n==1)return x;
    if(n%2==0)return exp(x*x, n/2);
    if(n%2==1)return x * exp(x*x, (n-1)/2); 

    return -1;
}

int main(){

    //ridicare in timp logaritmic
    fin>>n>>p;
    fout<<exp(n,p)%1999999973;


    return 0;
}