Cod sursa(job #1906749)

Utilizator KanghuAndre Popescu Kanghu Data 6 martie 2017 16:09:39
Problema Ridicare la putere in timp logaritmic Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 0.49 kb
#include <iostream>
#include <fstream>

using namespace std;

int P(int n, int p)
{
    if(p == 0)
        return 1;
    else if(p == 1)
        return n;

    else if(p % 2 == 0)
    {
        return P(n * n, p / 2) % 1999999973;
    }

    else if(p % 2 == 1)
    {
        return n * P(n * n, (p - 1) / 2) % 1999999973;
    }
}

int main()
{
    ifstream i("lgput.in");
    ofstream o("lgput.out");

    int n, p;

    i >> n >> p;

    o << P(n, p);
    return 0;
}