Cod sursa(job #877335)

Utilizator vgabi94Vaduva Gabriel vgabi94 Data 12 februarie 2013 19:40:01
Problema Ridicare la putere in timp logaritmic Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.49 kb
#include <fstream>
using namespace std;

typedef long long int int64;
ifstream in("lgput.in");
ofstream out("lgput.out");

const int K = 1999999973;
int64 N, P;

int64 expo(int64 p)
{
    int64 c;
    if (p == 0)
        return 1;
    else if (p % 2 == 0)
    {
        c = expo(p / 2);
        return (c * c) % K;
    }
    else
    {
        c = expo(p - 1);
        return (N * c) % K;
    }
}

int main()
{
    in >> N >> P;
    out << expo(P);
    return 0;
}