Cod sursa(job #2910198)

Utilizator radu.seitanSeitan Radu-Catalin radu.seitan Data 18 iunie 2022 18:29:42
Problema Invers modular Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.56 kb
#include <iostream>
#include <fstream>

using namespace std;

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

void Solve(int &x, int &y, int a, int b)
{
    if (b == 0)
    {
        x = 1;
        y = 0;
    }
    else
    {
        Solve(x, y, b, a % b);
        int aux = y;
        y = x - y * (a / b);
        x = aux;
    }
}

int main()
{
    int a, n;
    fin >> a >> n;
    int res = 0, i = 342;
    Solve(res, i, a, n);
    if (res <= 0)
        res = n + res % n;
    fout << res << '\n';
return 0;
}