Cod sursa(job #2144579)

Utilizator andrei32576Andrei Florea andrei32576 Data 26 februarie 2018 20:19:10
Problema Invers modular Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.55 kb
#include<bits/stdc++.h>
using namespace std;

int a,n,d,x,y;

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

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

        x=y0;
        y=x0-(a/b)*y0;
        return d;
    }
}

int main()
{
    fin>>a>>n;

    d=euclid(a,n,x,y);

    if(x<=0)
        x=x%n+n;

    fout<<x;

    fin.close();
    fout.close();
    return 0;
}