Cod sursa(job #2596000)

Utilizator AlexVulpoiuAlexandru Vulpoiu AlexVulpoiu Data 8 aprilie 2020 22:47:28
Problema Invers modular Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.57 kb
#include <fstream>

using namespace std;

ifstream f("inversmodular.in");
ofstream g("inversmodular.out");

int n, a, inv, y;

void euclid_extins(int a, int b, int &x, int &y)
{
    int x0, y0;

    if(!b)
    {
        x = 1;
        y = 0;
    }
    else
    {
        euclid_extins(b, a % b, x0, y0);
        x = y0;
        y = x0 - a / b * y0;
    }

}

int main()
{
    f >> a >> n;
    f.close();

    euclid_extins(a, n, inv, y);

    if(inv < 0)
        inv += n;
    inv %= n;

    g << inv << '\n';
    g.close();

    return 0;
}