Cod sursa(job #2391649)

Utilizator Costy_Suruniuc Constantin Costy_ Data 29 martie 2019 08:54:56
Problema Ridicare la putere in timp logaritmic Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.73 kb
#include <iostream>
#include <fstream>

using namespace std;
const int m = 1999999973;
long long exp(long long a, int b)
{
	if (b == 0)
		return 1;
	if (b == 1)
		return a;
	if (b % 2 == 0)
	{
		return exp(a * a, b / 2) % m;
	}
	else
	{
		return a * exp(a * a, (b - 1) / 2) % m;
	}
}

long long exp_it(long long a, int b)
{
	long long pwr = 1;
		
	while(b)
	{
		if (b % 2 == 1)
		{
			pwr = (pwr * a) % m;
		}
		a = (a * a) % m;
		b = b / 2;
		
		
	}
	return pwr;
}

int main()
{
	fstream inFile;
	inFile.open("lgput.in");
	long long a;
	int b;
	inFile >> a >> b;
	inFile.close();
	ofstream outFile;
	outFile.open("lgput.out");
	long long rez = exp_it(a, b);
	outFile << rez;
	outFile.close();
}