Cod sursa(job #2591888)

Utilizator mihai03Mihai Grigore mihai03 Data 31 martie 2020 17:07:03
Problema Invers modular Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.62 kb
#include <fstream>
using namespace std;
ifstream fin("inversmodular.in");
ofstream fout("inversmodular.out");
int a, n;
void Read()
{
    fin >> a >> n;
    fin.close();
}
void EuclidExtins(int a, int b, int &x, int &y)
{
    if(b == 0)
    {
        x = 1;
        y = 0;
    }
    else
    {
        int x0, y0;
        EuclidExtins(b, a % b, x0, y0);
        x = y0;
        y = x0 - (a / b) * y0;
    }
}
int main()
{
    Read();
    int x, y;
    EuclidExtins(a, n, x, y);
    int inversmodular = x;
    while(inversmodular < 0) inversmodular += n;
    fout << inversmodular;
    return 0;
}