Cod sursa(job #2370067)

Utilizator Fatu_SamuelFatu Samuel Fatu_Samuel Data 6 martie 2019 10:34:36
Problema Invers modular Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.59 kb
#include <iostream>
#include <fstream>

using namespace std;

ifstream fin("inversmodular.in");
ofstream fout("inversmodular.out");

int InvMod(int a, int b)
{
    long long x1 = 1, x2 = 0, aux, q, auxb = b;

    while(b)
    {
        q = a / b;
        aux = b;
        b = a - q * b;
        a = aux;
        aux = x2;
        x2 = x1 - q * x2;
        x1 = aux;
    }

    if(x1 < 0)
    {
        x1 += auxb;
    }

    return x1;
}

int main()
{
    int a, b;

    fin >> a >> b;

    fout << InvMod(a, b);

    fin.close();
    fout.close();
    return 0;
}