Cod sursa(job #2470823)

Utilizator hax_m8Nicolae Antonio Cristian hax_m8 Data 9 octombrie 2019 19:49:57
Problema Invers modular Scor 50
Compilator cpp-64 Status done
Runda basic_stuff Marime 0.72 kb
#include <fstream>

using namespace std;

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

int euclext(unsigned long long a, unsigned long long b, unsigned long long c, unsigned long long &x, unsigned long long &y)
{
    unsigned long long q[50], x0, y0, r, pos = 0;
    while(b)
    {
        q[++pos] = a / b;
        r = a % b;
        a = b;
        b = r;
    }
    if(c % a) {x=y=0;return 0;}
    x0 = c / a;
    y0 = 0;
    while(pos)
    {
        x = y0;
        y = x0 - q[pos--] * y0;
        x0 = x;
        y0 = y;
    }
    return 1;
}

int main()
{
    unsigned long long a, b, x, y;
    fin >> a >> b;
    euclext(a, b, 1, x, y);
    fout << x;
    return 0;
}