Cod sursa(job #2470834)

Utilizator hax_m8Nicolae Antonio Cristian hax_m8 Data 9 octombrie 2019 19:54:40
Problema Invers modular Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.65 kb
#include <fstream>

using namespace std;

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

int euclext(int a, int b, int c, int &x, int &y)
{
    int 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()
{
    int a, b, x, y;
    fin >> a >> b;
    euclext(a, b, 1, x, y);
    x=((long long)b+x%b)%b;
    fout << x;
    return 0;
}