Cod sursa(job #2164096)

Utilizator AndreiBadescuBadescu Andrei-Octavian AndreiBadescu Data 12 martie 2018 21:30:06
Problema Invers modular Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.49 kb
#include <fstream>

using namespace std;

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

int x, y, a, n;

void gcd ( int a, int b, int &x, int &y )
{
    if ( b == 0 )
    {
        x = 1;
        y = 0;
        return;
    }

    int x0, y0, t = a / b;

    gcd ( b, a - b * t, x0, y0 );

    x = y0;
    y = x0 - t * y0;
}

int main()
{
    fin >> a >> n;

    gcd ( a, n, x, y );

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

    fout << x;
}