Cod sursa(job #1518231)

Utilizator glbglGeorgiana bgl glbgl Data 5 noiembrie 2015 19:21:30
Problema Ridicare la putere in timp logaritmic Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.5 kb
#include <stdio.h>
#include <fstream>

#define DIVISOR 1999999973

using namespace std;

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


long long LgPut(long long N, long long P){

	if(P == 0) return 1;
	if(P == 1) return N;
	long long Y = 1;
	while(P > 1){
		if(P % 2 == 0){
			N = N * N % DIVISOR;
			P /= 2;
		}
		else{
			Y = Y * N % DIVISOR;
			N = N * N % DIVISOR;
			P = (P-1)/2;
		}
	}
	return N*Y % DIVISOR;
}

int main(){

	long long N,P; 
	in >> N >> P;
	out << LgPut(N,P) << "\n";

	return 0;
}