Cod sursa(job #2255013)

Utilizator HerddexJinga Tudor Herddex Data 6 octombrie 2018 12:22:13
Problema Ridicare la putere in timp logaritmic Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.5 kb
#include <fstream>
using namespace std;
typedef unsigned long long ull;
const ull Z = 1999999973;
ifstream fin("lgput.in");
ofstream fout("lgput.out");

ull powerFunction(ull n, ull p)
{
    if(p==0)
        return 1;
    if(p%2==1)
        return (powerFunction(n,p-1) * n)%Z;
    ull halfPower = (powerFunction(n,p/2))%Z;
    return (halfPower * halfPower)%Z;
}

int main()
{
    ull N, P;
    fin >> N >> P;

    fout << powerFunction(N%Z,P);

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