Cod sursa(job #1098930)

Utilizator malina_floreaMalina Florea malina_florea Data 5 februarie 2014 12:37:52
Problema Invers modular Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.54 kb
#include <fstream>
using namespace std;
ifstream fin ("inversmodular.in");
ofstream fout ("inversmodular.out");

int n;
int Euclid(int, int, int&, int&);

int main()
{
    int a, b;
    int x, y;

    fin>> a>> b;
    Euclid (a, b, x, y);

    while (x<0) x=x+b;
    fout<< x<< "\n";

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

int Euclid (int a, int b, int& x, int& y)
{
    int x0, y0, val;
    if (b==0) {x=1; y=0; return a;}

    val = Euclid(b, a%b, x0, y0);
    x=y0;
    y=x0-y0*(a/b);
    return val;
}