Cod sursa(job #255912)

Utilizator alex.cepoiAlexandru Cepoi alex.cepoi Data 10 februarie 2009 21:04:25
Problema Ridicare la putere in timp logaritmic Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 0.42 kb
#include <iostream>
using namespace std;
const unsigned int M=1999999973;

unsigned int N, P;

unsigned int factor (unsigned int N, unsigned int P)
{
	if (!P) return 1;
	else
	{
		unsigned int temp = factor (N,P/2);
		if (P%2) return (N*temp*temp) % M;
		else return (temp*temp) % M;
	}
}

int main ()
{
	freopen ("lgput.in","r",stdin);
	freopen ("lgput.out","w",stdout);

	cin>>N>>P;
	cout << factor (N,P)<<'\n';
	return 0;
}