Cod sursa(job #1349904)

Utilizator Tzappy90Mihalache Constantin Tzappy90 Data 20 februarie 2015 15:53:55
Problema Ridicare la putere in timp logaritmic Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.93 kb
#include <iostream>
#include <fstream>
#include <cmath>

using namespace std;
#define LIMIT 1999999973

long long int fast_exp(unsigned int base, unsigned int exp)
{
    if(exp==1)
    return base;
    else
    {
        if(exp%2 == 0)
        {
            long long int base1 = pow(fast_exp(base, exp/2),2);
            if(base1 >= LIMIT)
            return base1%LIMIT;
            else
            return base1;
        }
        else
        {
            long long int ans = (base*  pow(fast_exp(base,(exp-1)/2),2));
            if(ans >= LIMIT)
            return ans%LIMIT;
            else
            return ans;
        }
    }
}

long long put(long long int x, long long int y)
{
	if(y==0) return 1;
	if(y==1) return x;
	if(y%2==0) return put(x*x,y/2);
	return x*put(x*x,(y-1)/2);
}

int main()
{
	ifstream in("lgput.in");
	ofstream out("lgput.out");

	unsigned int x, y;

	cin >> x >> y;
	
	cout << fast_exp(x,y) << '\n';

	return 0;
}