Cod sursa(job #2822880)

Utilizator tiut_cristianTiut Cristian tiut_cristian Data 26 decembrie 2021 11:38:34
Problema Invers modular Scor 50
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.54 kb
#include <bits/stdc++.h>

using namespace std;

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

void Euclid(int a, int b, long long & x, long long & y)
{
    if(b == 0)
    {
        x = 1;
        y = 0;
    }
    else
    {
        long long x1, y1;
        Euclid(b, a % b, x1, y1);
        x = y1;
        y = x1 - y1 * (a / b);
    }
}

int main()
{
    int a, n;
    fin >> a >> n;
    long long x, y;
    Euclid(a, n, x, y);
    if(x <= 0)
        x += y;
    fout << x;

    return 0;
}