Cod sursa(job #1254803)

Utilizator TeodorPPaius Teodor TeodorP Data 3 noiembrie 2014 15:29:37
Problema Ridicare la putere in timp logaritmic Scor 20
Compilator cpp Status done
Runda Arhiva educationala Marime 0.6 kb
#include <fstream>
using namespace std;
ifstream is("lgput.in");
ofstream os("lgput.out");
#define Mod 1999999973
int x, n;
long long p;
int s;
void Power(int n, int x);

int main()
{
    is >> x >> n;
    p = 2;
    Power(n, x);
    os << p;
    is.close();
    os.close();
    return 0;
}

void Power(int n, int x)
{
    if(n == 0)
    {
        p = 1;
        return;
    }
    if(n == 1)
    {
        return;
    }

    if(n % 2 == 0)
    {
        Power(n/2, x);
        p *= p ;
        p %= Mod;

    }
    if(n % 2 == 1)
    {
        Power((n-1), x);
        p *= x;
        p %= Mod;

    }

}