Cod sursa(job #1179754)

Utilizator MarianMMorosac George Marian MarianM Data 29 aprilie 2014 10:51:25
Problema Ridicare la putere in timp logaritmic Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 0.42 kb
#include <fstream>
using namespace std;

#define PRIME 1999999973

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

long int expFast(int n, int p){
	if (p <= 1)		return n % PRIME;
	else if (p % 2)	return ((n % PRIME) * expFast((n * n) % PRIME, (p - 1) / 2)) % PRIME;
	else			return expFast((n * n) % PRIME, p / 2) % PRIME;
}

int main(){
	int n, p;

	fin >> n >> p;

	fout << expFast(n, p);

	return 0;
}