Cod sursa(job #933903)

Utilizator Paula-ElenaPaula-Elena Margarit Paula-Elena Data 30 martie 2013 13:20:52
Problema Invers modular Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.63 kb
#include<fstream>
using namespace std;

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

int a, b, x, y, c, d;

void euclid(int a, int b, int &x, int &y, int &d){
    if(b == 0){
        d = a;
        x = 1;
        y = 0;
        return;
    }
    int xx, yy, q;
    q = a/b;
    euclid(b, a%b, xx, yy, d);
    x = yy;
    y = xx - q*yy;
}

int main(){
    int c = 1;
    fin >> a >> b;
    euclid(a, b, x, y, d);
    int q = c/d;
    x = x*q;
    while(x < 1)
        x+= b;
    while(x > b-1)
        x-= b;

    fout << x;

    fin.close();
    fout.close();

    return 0;
}