Cod sursa(job #1507673)
| Utilizator | Data | 21 octombrie 2015 20:02:02 | |
|---|---|---|---|
| Problema | Ridicare la putere in timp logaritmic | Scor | 10 |
| Compilator | cpp | Status | done |
| Runda | Arhiva educationala | Marime | 0.6 kb |
#include <iostream>
#include <cstdio>
using namespace std;
int x,p1,p,V[100000],k,i,j;
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;
k=1;
p1=1;
while (p>=(p1<<1))
{
x=x*x;
p1<<=1;
k++;
V[k]=x;
}
// for (i=0;i<=k;i++) cout<<V[i]<<" ";
for (i=k;i>=0;i--)
{
j=1<<i;
// cout<<j<<" ";
if (p1+j<=p)
{
x=x*V[i+1];
p1+=j;
}
}
cout<<x;
}
