Cod sursa(job #1610571)

Utilizator ciprianprohozescuProhozescu Ciprian ciprianprohozescu Data 23 februarie 2016 17:29:18
Problema Invers modular Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.57 kb
#include <fstream>

using namespace std;

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

int euclid(int a, int b, int &x, int &y);

int main()
{
    int a, n, x, y, d;
    fin >> a >> n;
    d = euclid(a, n, x, y);
    while (x < 0)
        x += n;
    fout << x << '\n';
    return 0;
}

int euclid(int a, int b, int &x, int &y)
{
    int x1, y1;
    if (!b)
    {
        x = 1;
        y = 0;
        return a;
    }
    else
    {
        euclid(b, a % b, x1, y1);
        x = y1;
        y = x1 - (a / b) * y1;
    }
}