Cod sursa(job #1460952)

Utilizator borcanirobertBorcani Robert borcanirobert Data 14 iulie 2015 14:08:25
Problema Invers modular Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.62 kb
#include <fstream>
using namespace std;

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

int A, N;

void EuclidExtins( long long& x, long long& y, int a, int b );

int main()
{
    long long x, y;

    fin >> A >> N;
    EuclidExtins( x, y, A, N );

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

    fout << x << '\n';

    fin.close();
    fout.close();
    return 0;
}

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