Cod sursa(job #1325512)
Utilizator | Data | 24 ianuarie 2015 00:25:43 | |
---|---|---|---|
Problema | Ridicare la putere in timp logaritmic | Scor | 10 |
Compilator | cpp | Status | done |
Runda | Arhiva educationala | Marime | 0.43 kb |
#include <cstdio>
using namespace std;
int lgexp(int x,int n)
{
if(x==1||n==0) return 1;
else if(n==1) return x;
else if(x%2==0)
return lgexp(x*x,n/2);
else if(x%2!=0)
return x*lgexp(x*x,(n-1)/2);
}
int main()
{
int x,n;
freopen("lgput.in","r",stdin);
freopen("lgput.out","w",stdout);
scanf("%d%d",&x,&n);
printf("%d",lgexp(x,n));
return 0;
}