Cod sursa(job #2105927)

Utilizator xRoALexBirtoiu Alexandru xRoALex Data 14 ianuarie 2018 17:31:13
Problema Invers modular Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.49 kb
#include <fstream>

using namespace std;

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

int a,n;
long long x,y;

void euclid(long long &x,long long &y, int a,int b)
{
    if(!b)
    {
        x=1;
        y=0;
    }
    else
    {
        euclid(x,y,b,a%b);
        long long aux = x;
        x=y;
        y= aux- y* (a/b);
    }
}

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