Cod sursa(job #1001123)

Utilizator alexx.cosmaCosma Cristian Alexandru alexx.cosma Data 24 septembrie 2013 15:48:15
Problema Ridicare la putere in timp logaritmic Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 0.45 kb
#include <iostream>
#include <fstream>

using namespace std;

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

int weird = 1999999973;

int powLog(int n, int p);

int main()
{

    int n,p;

    fin>>n;
    fin>>p;
    fout << powLog(n,p)% weird;

    return 0;
}

int powLog(int n, int p)
{
    if(p==0) return 1;
    else if(p==1) return n;
    else if(p%2==0) return powLog(n*n,p/2);
    else return n*powLog(n*n,(p-1)/2);
}