Cod sursa(job #1378404)

Utilizator Corina1997Todoran Ana-Corina Corina1997 Data 6 martie 2015 12:01:01
Problema Invers modular Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.57 kb
#include <fstream>
using namespace std;

ifstream is("inversmodular.in");
ofstream os("inversmodular.out");

int n, a, b;
void Euclid( int a, int b, int& x, int &y );

int main()
{
    is >> a >> b;
    int x, y;
    Euclid( a, b, x, y );
    if ( x <= 0 )
        x += b;
    os << x;
    is.close();
    os.close();
    return 0;
}

void Euclid( int a, int b, int& x, int &y )
{
    if ( b == 0 )
    {
        x = 1;
        y = 0;
        return;
    }
    int x0, y0;
    Euclid( b, a % b, x0, y0 );
    x = y0;
    y = x0 - ( a / b ) * y0;
}