Cod sursa(job #2048185)

Utilizator zanugMatyas Gergely zanug Data 25 octombrie 2017 19:58:18
Problema Invers modular Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.61 kb
#include <iostream>
#include <fstream>

using namespace std;

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

int n, a;

void eucex(int a, int b, int& x, int& y, int& d)
{
    if(!b)
    {
        d = a;
        x = 1;
        y = 0;
    }
    else
    {
        int xx, yy;
        eucex(b, a%b, xx, yy, d);
        x = yy;///!
        y = xx - (a / b) * yy;///!
    }
}

int main()
{
    ios::sync_with_stdio(false);

    fin >> a >> n;

    int x, y, d;

    eucex(a, n, x, y, d);

    while(x < 0)
        x += n;

    fout << x << "\n";

    return 0;
}