Cod sursa(job #1507568)
| Utilizator | Data | 21 octombrie 2015 19:06:12 | |
|---|---|---|---|
| Problema | Ridicare la putere in timp logaritmic | Scor | 10 |
| Compilator | cpp | Status | done |
| Runda | Arhiva educationala | Marime | 0.51 kb |
#include <iostream>
#include <cstdio>
using namespace std;
int x,p1,p,V[100000],k,i;
int main()
{
// std::ios::sync_with_stdio(false);
freopen("lgput.in","r",stdin);
freopen("lgput.out","w",stdout);
cin>>x>>p;
V[1]=x;
V[0]=1;
p1=1;
while (p>=(p1<<1))
{
x=x*x;
p1<<=1;
k++;
V[k]=x;
}
for (i=k;i>0&&p1<p;i--)
{
if ((p1+1<<i)<p)
{
x*=V[i];
p1+=1<<k;
}
}
cout<<x;
}
