Cod sursa(job #2230276)

Utilizator nicolaefilatNicolae Filat nicolaefilat Data 9 august 2018 17:00:59
Problema Invers modular Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.5 kb
#include <iostream>
#include <fstream>

using namespace std;

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

void euclid(int &x,int &y,int a,int b){
    if(!b){
        x = 1;
        y = 0;
        return ;
    }
    euclid(x,y,b,a%b);
    int newx = y;
    int newy = x - y * (a/b);
    x = newx;
    y = newy;
}
int main()
{
    int x,y,a,b,d;
    in>>a>>b;
    euclid(x,y,a,b);
    x %= b;
    if(x < 0)
        x += b;

    out<<x;
    return 0;
}