Cod sursa(job #2906802)

Utilizator Tony2003Tony Tamas Tony2003 Data 27 mai 2022 15:04:19
Problema Ridicare la putere in timp logaritmic Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.44 kb
#include<iostream>
#include<fstream>

using namespace std;

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

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

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

    fout << exp(n, p) % 1999999973;


    return 0;
}