Cod sursa(job #1292158)

Utilizator PTAdrian64Pop-Tifrea Adrian PTAdrian64 Data 13 decembrie 2014 19:01:23
Problema Ridicare la putere in timp logaritmic Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 0.5 kb
#include <cstdio>
#define ll long long

using namespace std;

ll n,p;

void log_exp(ll a,ll b){
    ll x = 1,p = 1;
    while(p <= b)p = p<<1;
    p = p>>1;
    while(p){
        if(p <= b){
            x *= x * a;
            b -= p;
        }
        else
        x *= x;
        p = p>>1;
    }
    printf("%lld\n",x);
}

int main()
{
    freopen("lgput.in","r",stdin);
    freopen("lgput.out","w",stdout);
    scanf("%lld %lld ",&n,&p);
    log_exp(n,p);

    return 0;
}