Cod sursa(job #1901342)

Utilizator TheRenegateMoldovanu Dragos TheRenegate Data 3 martie 2017 21:24:06
Problema Ridicare la putere in timp logaritmic Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 0.45 kb
#include <fstream>
using namespace std;
ifstream fin("lgput.in");
ofstream fout("lgput.out");

long long lgput(int n, int p)
{
    if(p==0)
    {
        return 1;
    }
    if(p==1)
    {
        return n;
    }
    if(p%2==0)
    {
        return lgput(n*n,p/2)%1999999973;
    }
    else
    {
        return n*lgput(n*n,p/2)%1999999973;
    }
}

int main()
{
    int n,p;
    fin>>n>>p;
    fout<<lgput(n,p);
    return 0;
}