Cod sursa(job #3005121)

Utilizator indianu_talpa_iuteTisca Catalin indianu_talpa_iute Data 16 martie 2023 19:34:29
Problema Invers modular Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.46 kb
#include <bits/stdc++.h>
#define LL long long

using namespace std;

ifstream fin("inversmodular.in");
ofstream fout("inversmodular.out");

void gcd(LL& x, LL& y, LL a, LL b) {
    if (b == 0) {
        x = 1;
        y = 0;
        return;
    }

    gcd(x, y, b, a % b);
    LL aux = x;
    x = y;
    y = aux - a / b * y;
}

int main() {
    LL a, n, inv, y;
    fin >> a >> n;
    gcd(inv, y, a, n);
    while (inv < 0)
        inv += n;
    fout << inv;
    return 0;
}