Cod sursa(job #2626829)

Utilizator As932Stanciu Andreea As932 Data 8 iunie 2020 16:10:07
Problema Invers modular Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.51 kb
#include <fstream>

using namespace std;

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

typedef long long ll;

ll gcd(ll &x, ll &y, ll a, ll 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 a, n, inv = 0, ins;

    cin >> a >> n;

    gcd(inv, ins, a, n);

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

    cout << inv;

    return 0;
}