Cod sursa(job #2476776)

Utilizator MarianConstantinMarian Constantin MarianConstantin Data 19 octombrie 2019 11:26:36
Problema Invers modular Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.52 kb
#include <iostream>
#include <fstream>
#define ll long long

using namespace std;

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

void gcdExtended(ll a, ll b, ll &x, ll &y)  {
    if (!b) {
        x = 1;
        y = 0;
        return;
    }
    gcdExtended(b, a % b, x, y);
    ll aux = x;
    x = y;
    y = aux - (a / b) * y;
}

int main() {
    ll a, n, x, y;
    fin >> a >> n;
    gcdExtended(a, n, x, y);
    if (x < 0)
        x += n;
    fout << x;
    return 0;
}