Cod sursa(job #2933954)

Utilizator andreiiorgulescuandrei iorgulescu andreiiorgulescu Data 5 noiembrie 2022 13:54:51
Problema Invers modular Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.6 kb
#include <bits/stdc++.h>

using namespace std;

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

void euclid(int a,int b,int &x,int &y,int &d)
{
    if (b == 0)
    {
        d = a;
        x = 1;
        y = 0;
    }
    else
    {
        int x0,y0;
        euclid(b,a % b,x0,y0,d);
        x = y0;
        y = x0 - (a / b) * y0;
    }
}

int main()
{
    int a,n,x,y,d;
    in >> a >> n;
    euclid(a,n,x,y,d);
    if (x < 0)
    {
        int dv = x / n;
        x += (dv * n);
        if (x < 0)
            x += n;
    }
    out << x;
    return 0;
}