Cod sursa(job #1893510)

Utilizator patrutoiuandreipatrutoiu andrei patrutoiuandrei Data 25 februarie 2017 18:53:51
Problema Invers modular Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.55 kb
#include <fstream>

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