Cod sursa(job #2873688)

Utilizator AlexMoto2006Motoasca Alexandru-Lucian AlexMoto2006 Data 19 martie 2022 11:21:08
Problema Invers modular Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.53 kb
#include <fstream>
typedef long long ll;

using namespace std;

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

void euclid(ll a, ll b, ll& x, ll& y)
{
    if (b == 0)
    {
        x = 1, y = 0;
        return;
    }
    else
    {
        ll x1, y1;
        euclid(b, a % b, x1, y1);
        x = y1;
        y = x1 - a / b * y1;
    }
}

int main()
{
    ll a, b, x, y;
    cin >> a >> b;
    euclid(a, b, x, y);
    if (x <= 0)
        x = b + x % b;
    cout << x;
    return 0;
}