Cod sursa(job #3272039)

Utilizator mihaihvhTuburlui Mihai mihaihvh Data 28 ianuarie 2025 10:41:55
Problema Invers modular Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.6 kb
#include <fstream>

using namespace std;

#define ll long long

ifstream cin("inversmodular.in");
ofstream cout("inversmodular.out");

ll euclid(ll a, ll b, ll &x, ll &y) {
    if (b == 0) {
        x = 1;
        y = 0;
        return a;
    }
    ll x0, y0;
    ll d = euclid(b, a % b, x0, y0);
    x = y0;
    y = x0 - (a / b) * y0;
    return d;
}

int main() {
    cin.tie(0);
    cin.sync_with_stdio(false);

    ll a, b;
    cin >> a >> b;
    ll inva, invb;
    euclid(a, b, inva, invb);
    if (inva <= 0)
        inva = b + inv % b;
    cout << inva;
    return 0;
}