Cod sursa(job #2539977)

Utilizator BluThund3rRadu George BluThund3r Data 6 februarie 2020 16:45:36
Problema Ridicare la putere in timp logaritmic Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.59 kb
#include <fstream>
using namespace std;

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

int n, exp;

int quick_power(int base, int exponent)
{
    int result = 1;

    while(exponent > 1)
    {
        if(exponent % 2 == 0)
        {
            exponent /= 2;
            base *= base;
        }

        else
        {
            exponent = exponent / 2 - 1;
            result *= base;
            base *= base;
        }
    }

    return result * base;
}

int main()
{
    in >> n >> exp;

    out << quick_power(n, exp) % 1999999973;

    return 0;
}