Cod sursa(job #1580842)

Utilizator dragos99Homner Dragos dragos99 Data 26 ianuarie 2016 10:38:42
Problema Ridicare la putere in timp logaritmic Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 0.39 kb
#include<fstream>
using namespace std;
long n,p;
long exp(long x, long y)
{
    x=x%1999999973;
    if(y==0)
        return 1;
    else if(y==1)
        return x;
    else if(y%2==0)
        return exp(x*x,y/2);
    else if(y%2==1)
        return x*exp(x*x,(y-1)/2);
}
int main()
{
    ifstream f("lgput.in");
    ofstream g("lgput.out");
f>>n>>p;
g<<exp(n,p);
return 0;
}