Cod sursa(job #2414708)

Utilizator pimao2004Lupu Stefan Dragos pimao2004 Data 24 aprilie 2019 22:18:57
Problema Invers modular Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.48 kb
#include <fstream>

using namespace std;
ifstream in ("inversmodular.in");
ofstream out ("inversmodular.out");
void euclid(int a,int b,long long &x,long long &y)
{
    if(b==0)
    {
        y=0;
        x=1;
    }
    else
    {
        euclid(b,a%b,x,y);
        long long d=x;
        x=y;
        y=d-y*(a/b);
    }
}
int main()
{
    int a,n;
    in>>a>>n;
    long long x,y;
    euclid(a,n,x,y);
    if(x<0)
        x=n+x%n;
    out<<x;
    return 0;
}