Cod sursa(job #2929683)

Utilizator AleooPascaru Alexandru Aleoo Data 26 octombrie 2022 15:25:33
Problema Invers modular Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.51 kb
#include <fstream>

using namespace std;

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

#define ll long long

int a, n;

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

int main()
{
    ll inv = 0, ins;

    fin >> a >> n;

    gcd(inv, ins, a, n);

    if (inv <= 0)
        inv = n + inv % n;

    fout << inv;

    return 0;
}