Cod sursa(job #1381950)

Utilizator gabi.cristacheGabi Cristache gabi.cristache Data 8 martie 2015 13:39:50
Problema Ridicare la putere in timp logaritmic Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 0.52 kb
#include <iostream>
#include <fstream>

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

long long myExponential(long long n, long long p) {
	if (p == 0) {
		return 1;
	} else if (p == 1) {
		return n;
	} else if (p % 2 == 0) {
		long long res = myExponential(n, p / 2);
		return res * res;
	} else {
		long long res = myExponential(n, (p - 1) / 2);
		return res * res * n;
	}
}

int main() {
	long long k = 1999999973;
	long long n, p;
	fin >> n >> p;

	fout << myExponential(n, p) % k << std::endl;

	return 0;
}