Cod sursa(job #3030090)

Utilizator stefan2806Radulescu Stefan stefan2806 Data 17 martie 2023 15:14:19
Problema Invers modular Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.58 kb
#include <fstream>

using namespace std;

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

long long a,n,b,x,y,d;

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

int main()
{
    cin>>a>>n;
    b=n;
    d=euclid(a,b,x,y);
    if(d==1)
    {
        while(x<0)
            x+=n;
        cout << x << endl;
    }
    return 0;
}