Cod sursa(job #3350671)

Utilizator Alex_at_gameIustin-Alexandru Frateanu Alex_at_game Data 11 aprilie 2026 18:18:50
Problema Invers modular Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.88 kb
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
#define all(v) begin(v), end(v)
#define al(v, l, r) begin(v) + l, begin(v) + r + 1
#define sz(v) (int)v.size()
#define pb push_back
#define pob pop_back
#define fs first
#define sd second

constexpr int inf = 2e9;
constexpr ll infll = 4e18;

ll gcde(ll a, ll b, ll& x, ll& y) {
    if (!b) {
        x = 1;
        y = 0;
        return a;
    }

    ll x0, y0, g = gcde(b, a % b, x0, y0);
    x = y0;
    y = x0 - (a / b) * y0;
    return g;
}

int a, n;
signed main() {
    ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
    freopen("inversmodular.in", "r", stdin);
    freopen("inversmodular.out", "w", stdout);

    cin >> a >> n;
    ll ans, _;
    gcde(a, n, ans, _);
    cout << ((ans % n) + n) % n << "\n";
}