Cod sursa(job #2632675)

Utilizator Joke2111Pricop Tudor Joke2111 Data 4 iulie 2020 13:00:13
Problema Ridicare la putere in timp logaritmic Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.61 kb
#include <bits/stdc++.h>

using namespace std;

const int x=1999999973;

long long n,p,temp;

void fast ()
{
    ios_base::sync_with_stdio(false);
    cin.tie();
}

long long put (long long a, long long b)
{
    if (b==0)
        return 1;

    if (b%2==0)
    {
        temp = put (a, b/2);
        return temp*temp;
    }

    if (b%2==1)
    {
        temp = put (a, (b-1)/2);
        return temp*temp*a;
    }
}

int main()
{
    fast();
    freopen("lgput.in","r", stdin);
    freopen("lgput.out","w", stdout);

    cin >> n >> p;

    cout << put(n,p);

    return 0;
}