Cod sursa(job #1730661)
Utilizator | AndreiCDG Andrei_Popa | Data | 17 iulie 2016 13:33:57 |
---|---|---|---|
Problema | Ridicare la putere in timp logaritmic | Scor | 10 |
Compilator | cpp | Status | done |
Runda | Arhiva educationala | Marime | 0.69 kb |
#include <iostream>
#include <fstream>
using namespace std;
ifstream f("lgput.in");
ofstream g("lgput.out");
long long exp_by_squaring(long long x,int n)
{
if(n<0)
return exp_by_squaring(1/x,-n);
else
{
if(n==0)
return 1;
else
{
if(n==1)
return x;
else
{
if(n%2==0)
return exp_by_squaring(x*x,n/2);
else
return x*exp_by_squaring(x*x,(n-1)/2);
}
}
}
}
int main()
{
int a,b;
f>>a>>b;
g<<exp_by_squaring(a,b)%1999999973;
f.close();
g.close();
return 0;
}