Cod sursa(job #2441497)

Utilizator AlexNeaguAlexandru AlexNeagu Data 20 iulie 2019 16:01:46
Problema Invers modular Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.51 kb
#include <fstream>
using namespace std;
ifstream cin ("inversmodular.in");
ofstream cout("inversmodular.out");
int a, b, c, d, x, y;
void gcd(int a, int b, int &x, int &y) {
    if (!b) {
        x = 1;
        y = 0;
    }
    else {
        int x0, y0;
        gcd(b, a % b, x0, y0);
        x = y0;
        y = x0 - (a / b) * y0;
    }
}
int main() {
    int a, n;
    cin >> a >> n;
    int inv, ins;
    gcd(a, n, inv, ins);
    if (inv <= 0) inv = n + inv % n;
    cout << inv;
    return 0;
}