Cod sursa(job #1522566)

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

using namespace std;

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

int n, k;

int rd(int x, int n);


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

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

int rd(int x, int 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);
}