Cod sursa(job #3336998)

Utilizator Radu_BicliBiclineru Radu Radu_Bicli Data 26 ianuarie 2026 20:20:05
Problema Invers modular Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.71 kb
#include <bits/stdc++.h>

using namespace std;

#define USE_STD_IO 0
#if USE_STD_IO
    #define fin cin
    #define fout cout
#else
    ifstream fin("inversmodular.in");
    ofstream fout("inversmodular.out");
#endif

int a, n, x, y;

static inline void EuclidExt(int a, int b, int& x, int& y) {
    if(0 == b) {
        x = 1;
        y = 0;
    }
    else {
        int x1, y1;
        EuclidExt(b, a % b, x1, y1);
        x = y1;
        y = x1 - (a / b) * y1;
    }
}

int main() {
    if(USE_STD_IO) ios_base::sync_with_stdio(false);
    fin.tie(NULL);
    fout.tie(NULL);

    fin >> a >> n;
    EuclidExt(a, n, x, y);
    while(x < 0) x += n;
    fout << x;

    return 0;
}