Cod sursa(job #898850)

Utilizator tanduraDomnita Dan tandura Data 28 februarie 2013 11:58:02
Problema Invers modular Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.47 kb
#include <iostream>
#include <fstream>
using namespace std;

void rez(int &x,int &y,int a,int b)
{
    if(b==0)
    {
        x=1;
        y=0;
    }
    else
    {
        rez(x,y,b,a%b);
        int aux=x;
        x=y;
        y=aux-y*(a/b);
    }
}

int main()
{
    int a,b,x,y;
    ifstream f("inversmodular.in");
    ofstream g("inversmodular.out");
    f>>a>>b;
    rez(x,y,a,b);
    if(x<=0)
        x=b+x%b;
    g<<x<<"\n";
    return 0;
}