Cod sursa(job #1318857)

Utilizator Marius_mFMI-M2 Marius Melemciuc Marius_m Data 16 ianuarie 2015 14:09:01
Problema Ridicare la putere in timp logaritmic Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.96 kb
/*
 * =====================================================================================
 *
 *       Filename:  pow_infoarena.cpp
 *
 *    Description:  
 *
 *        Version:  1.0
 *        Created:  01/16/2015 02:04:23 PM
 *       Revision:  none
 *       Compiler:  gcc/g++
 *
 *         Author:  Marius-Constantin Melemciuc  
 *          email:  
 *   Organization:  
 *
 * =====================================================================================
 */

#include <iostream>
#include <cstdio>
#define LARGE_NUM 1999999973

using namespace std;

int pow2(int x, int y)
{
	bool is_negative = false;

	if (y < 0)
	{
		is_negative = true;
		y = -y;
	}

	int result = 1;

	while (y != 0)
	{
		if ((y & 1) == 1) // is odd 
			result *= x;

		x = x * x;
		y = y >> 1;
	} /* while */

	return result;
}

int main(int argc, char** argv)
{
	ifstream input("lgput.in");
	ofstream output("lgput.out");

	int n, p;

	input >> n >> p;

	output << pow2(n, p) % LARGE_NUM;

	return 0;
}