Cod sursa(job #625324)
| Utilizator | Data | 24 octombrie 2011 11:55:38 | |
|---|---|---|---|
| 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;
ifstream f("lgput.in");
ofstream g("lgput.out");
int x,p;
int fastexp(int b, int n)
{
if(n==0) return 1;
if(n<0) return (1/fastexp(b,-n));
if(n%2)
{ int q=fastexp(b,(n-1)/2);
return b*q*q;
}
int q=fastexp(b,n/2);
return q*q;
}
int main()
{
f>>x>>p;
g<<fastexp(x,p)<<'\n';
f.close(); g.close();
return 0;
}
