Cod sursa(job #1459074)

Utilizator aimrdlAndrei mrdl aimrdl Data 9 iulie 2015 01:02:14
Problema Ridicare la putere in timp logaritmic Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.46 kb
#include <iostream>
#include <fstream>

#define M 1999999973

using namespace std;

long long power (long long n, long long p) {	
	long long r = 1, temp = n, checkbit = 1;
	while (checkbit <= p) {
		if (checkbit & p) r = (r * temp) % M;
		temp = (temp * temp) % M;
		checkbit <<= 1;
	}
	
	return r;
}
	
	
	
int main (void) {
	freopen("lgput.in", "r", stdin);
	freopen("lgput.out", "w", stdout);
	
	long long n, p;
	cin >> n >> p;
	cout << power(n,p) % M;
	
	return 0;
}