Cod sursa(job #2211142)

Utilizator andaraluca2001Anda Epure andaraluca2001 Data 9 iunie 2018 14:14:17
Problema Invers modular Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.52 kb
#include <iostream>
#include <fstream>

using namespace std;
//a*x%MOD=1
//cmmdc(a, MOD) =1

int cmmdc(int a, int b, int &x, int &y)
{
    if(b==0)
    {
        x=1;
        y=0;
        return a;
    }

    int d=cmmdc(b,a%b,x,y);
    int aux=x;
    x=y;
    y=aux-y*(a/b);
    return d;
}

int main()
{
    ifstream cin("inversmodular.in");
    ofstream cout("inversmodular.out");
    int a ,mod,x, y;

    cin>>a>>mod;

    cmmdc(a,mod,x,y);
    while(x<0) x=x+mod;
    cout<<x;
    return 0;
}