Cod sursa(job #2678137)

Utilizator stefan2806Radulescu Stefan stefan2806 Data 28 noiembrie 2020 10:46:06
Problema Invers modular Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.46 kb
#include <fstream>

using namespace std;

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

int a,n,x,y;

void euclid_ext(int a,int n,int &x,int &y)
{
    int x0,y0;
    if(n==0)
    {
        x=1;
        y=0;
        return;
    }
    euclid_ext(n,a%n,x0,y0);
    x=y0;
    y=x0-(a/n)*y0;
}
int main()
{
    fin>>a>>n;
    euclid_ext(a,n,x,y);
    while(x<0)
        x+=n;
    fout << x << endl;
    return 0;
}