Cod sursa(job #1773580)

Utilizator alexandru.ghergutAlexandru-Gabriel Ghergut alexandru.ghergut Data 7 octombrie 2016 23:07:41
Problema Ridicare la putere in timp logaritmic Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.53 kb
#include <fstream>
using namespace std;

int modularPow(unsigned long long base, unsigned long long exp, int mod)
{
    int result = 1;
    while (exp)
    {
        if (exp & 1)
            result = ((result % mod) * (base % mod)) % mod;
        base = ((base % mod) * (base % mod)) % mod;
        exp >>= 1;
    }

    return result;
}

int main()
{
    unsigned long long N, P;
    ifstream f("lgput.in");
    f >> N >> P;
    f.close();

    ofstream g("lgput.out");
    g << modularPow(N, P, 1999999973);
    g.close();
    return 0;
}