Cod sursa(job #1503814)
Utilizator | AndreiCDG Andrei_Popa | Data | 16 octombrie 2015 23:18:13 |
---|---|---|---|
Problema | Ridicare la putere in timp logaritmic | Scor | 10 |
Compilator | cpp | Status | done |
Runda | Arhiva educationala | Marime | 0.62 kb |
#include <iostream>
#include <fstream>
using namespace std;
ifstream f("lgput.in");
ofstream g("lgput.out");
int exp(int x,int n)
{
if(n<0)
{
exp(1/x,n*(-1));
}
else
{
if(n==0)
return 1;
else
{
if(n==1)
return x;
else
{
if(n%2==0)
return exp(x*x,n/2);
else
return x*exp(x*x,(n-1)/2);
}
}
}
}
int main()
{
int x,n;
f>>x>>n;
g<<exp(x,n)%1999999973;
f.close();
g.close();
return 0;
}