Cod sursa(job #2756682)

Utilizator vlad2009Vlad Tutunaru vlad2009 Data 2 iunie 2021 12:15:31
Problema Invers modular Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.51 kb

#include <fstream>

using namespace std;

void euclid(long long a, long long b, long long &x, long long &y)
{
    if (b == 0)
    {
        x = 1;
        y = 0;
        return;
    }
    long long xx = x, yy = x;
    euclid(b, a % b, xx, yy);
    y = xx - (a / b) * yy;
    x = yy;
}

int main()
{
    ifstream fin("inversmodular.in");
    ofstream fout("inversmodular.out");
    long long a, n, x, y;
    fin >> a >> n;
    euclid(a, n, x, y);
    fout << (x % n + n) % n;
    return 0;
}