Cod sursa(job #2228475)

Utilizator andreisontea01Andrei Sontea andreisontea01 Data 3 august 2018 21:16:29
Problema Invers modular Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.56 kb
#include <iostream>
#include <fstream>

using namespace std;

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

int main()
{
    ifstream fin("inversmodular.in");
    ofstream fout("inversmodular.out");
    int a, n;
    fin >> a >> n;
    int x = 0, y = 0;
    euclidExt(a, n, 1, x, y);
    if(x < 0){
        int coef = -(x / n);
        x += n * (coef + 1);
    }
    fout << x;
    return 0;
}