Cod sursa(job #2422466)

Utilizator alexoloieriAlexandru Oloieri alexoloieri Data 18 mai 2019 21:22:59
Problema Invers modular Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.54 kb
#include <bits/stdc++.h>
#define lld long long
using namespace std;
// in x se va gasi inversul modular al lui a, Zb
lld invMod(lld &x, lld &y, int a, int b)
{
    if (!b)
        x = 1, y = 0;
    else
    {
        invMod(x, y, b, a%b);
        lld aux = x;
        x = y;
        y = aux - y*(a/b);
    }
}
lld x, y;
int a, b;
int main()
{
    freopen("inversmodular.in","r",stdin);
    freopen("inversmodular.out","w",stdout);
    cin>>a>>b;
    invMod(x,y,a,b);
    if (x < 0) x = b + x;
    cout<<x<<'\n';
    return 0;
}