Cod sursa(job #2270646)

Utilizator ianiIani Biro iani Data 27 octombrie 2018 12:05:31
Problema Invers modular Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.57 kb
#include <iostream>
#include <fstream>

using namespace std;

void euclid(int a, int b, int &d, int &x, int &y)
{
    if (b == 0)
    {
        if (d%a!=0)
            x=0;
        else
            x = d/a;
        y = 0;
    }
    else
    {
        int x0, y0;
        euclid(b, a % b, d, x0, y0);
        x = y0;
        y = x0 - (a / b) * y0;
    }
}

int main()
{
    ifstream fin ("inversmodular.in");
    ofstream fout ("inversmodular.out");
    int a,n,c,x,y;
    fin>>a>>n;
    c=1;
    euclid(a, n, c, x, y);
    while (x<0)
        x+=n;
    fout<<x<<'\n';
    return 0;
}