Cod sursa(job #1072769)

Utilizator sebinechitasebi nechita sebinechita Data 4 ianuarie 2014 20:38:34
Problema Invers modular Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.53 kb
#include <iostream>
#include <fstream>
using namespace std;
ifstream fin("inversmodular.in");
ofstream fout("inversmodular.out");

void cmmdc(int x, int y, int &a, int &b, int &gcd)
{
    int aa,bb;
    if(!y)
    {
        gcd=x;
        a=1;
        b=0;
        return;
    }

    cmmdc(y, x%y, aa, bb, gcd);
    a=bb;
    b=aa-(x/y)*bb;
}

int main()
{
    int a, n, rez, x=0, y=0;
    fin>>a>>n;
    cmmdc(a, n, x, y, rez);

    while(x<=0)
        x+=n;
    while(x>n)
        x-=n;
    fout<<x<<"\n";
}