Cod sursa(job #2008231)

Utilizator ArctopusKacso Peter-Gabor Arctopus Data 5 august 2017 20:36:49
Problema Invers modular Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.58 kb
#include <iostream>
#include <fstream>

using namespace std;

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

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

int a, n;

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

    int x, y;
    int d;
    euclid( a, n, &d, &x, &y );

    while( x < 0 )
        x += n;

    fout << x << "\n";

    return 0;
}