Cod sursa(job #1522569)

Utilizator blackoddAxinie Razvan blackodd Data 11 noiembrie 2015 20:16:56
Problema Ridicare la putere in timp logaritmic Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 0.51 kb
#include <fstream>

using namespace std;

ifstream fin("lgput.in");
ofstream fout("lgput.out");

#define ULL unsigned long long

ULL n, k;

ULL rd(ULL x, ULL n);


int main()
{
    fin >> n >> k;
    fout << rd(n,k);

    fin.close();
    fout.close();
    return 0;
}

ULL rd(ULL x, ULL n)
{
    if ( !n )
        return 1;
    if ( n == 1 )
        return x;
    if ( x % 2 == 0 )
        return rd(x * x, n / 2);
    if ( x % 2 == 1 )
        return x * rd( x * x, (n - 1) / 2);
}