Cod sursa(job #1124923)

Utilizator japjappedulapPotra Vlad japjappedulap Data 26 februarie 2014 14:27:49
Problema Invers modular Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 0.61 kb
#include <fstream>
#include <stack>
using namespace std;

ifstream is ("inversmodular.in");
ofstream os ("inversmodular.out");

int a, n, x;

int POW(int A, int B, int MOD);

int main()
{
    is >> a >> n;
    x = POW(a, n-2, n);
    os << x;

    is.close();
    os.close();
    return 0;
}

int POW(int A, int B, int MOD)
{
    stack <int> S;
    int aux = B;
    for (; aux; aux /= 2) S.push(aux % 2);
    for (aux = 1 ; !S.empty(); S.pop())
        if (S.top() == 0)   aux = ( 1LL * aux * aux) % MOD;
        else                aux = ( 1LL * aux * aux * A) % MOD;
    return aux;
};