Cod sursa(job #2606383)

Utilizator ruxiTTutunaru Ruxandra ruxiT Data 27 aprilie 2020 17:13:55
Problema Invers modular Scor 50
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.56 kb
#include <iostream>
#include <fstream>

using namespace std;

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

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


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