Cod sursa(job #2849832)

Utilizator stefan05Vasilache Stefan stefan05 Data 15 februarie 2022 20:45:30
Problema Invers modular Scor 50
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.6 kb
#include <fstream>

#define LL long long int

using namespace std;

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

LL a, n;
LL x, y, d;

void euclidExtins(LL, LL, LL&, LL&, LL&);

int main()
{
    fin >>a>>n;
    euclidExtins(a, n, x, y, d);
    if (x < 0) x = x + ((-x) / n * n);
    fout <<x<<'\n';
    fout.close();
    return 0;
}

void euclidExtins(LL a, LL b, LL& x, LL& y, LL& d)
{
    if (b == 0)
    {
        d = a; x = 1; y = 0;
        return;
    }

    LL x0, y0;
    euclidExtins(b, a%b, x0, y0, d);
    x = y0; y = x0 - (a / b) * y0;
}