Cod sursa(job #3358378)

Utilizator SeracovanuEdwardSeracovanu Edward SeracovanuEdward Data 16 iunie 2026 16:11:16
Problema Invers modular Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.59 kb
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

void gcd(int a, int b, ll& x, ll& y) {
    if (b == 0) {
        x = 1;
        y = 0;
    } else {
        gcd(b, a % b, x, y);
        ll temp = x;
        x = y;
        y = temp - (a / b) * y;
    }
}

int main() {
    freopen("inversmodular.in", "r", stdin);
    freopen("inversmodular.out", "w", stdout);
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int A, N;
    ll inv = 0, ins;
    cin >> A >> N;
    gcd(A, N, inv, ins);
    if (inv <= 0) inv = N + inv % N;
    cout << inv << "\n";
}