Cod sursa(job #1594619)

Utilizator tudi98Cozma Tudor tudi98 Data 9 februarie 2016 16:57:32
Problema Invers modular Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.59 kb
#include <fstream>
using namespace std;

int ExtendedGCD(int a,int b,int& x,int& y)
{
    if (b == 0) {
        x = 1;
        y = 0;
        return a;
    }
    else {
        int x0,y0;
        int d = ExtendedGCD(b,a%b,x0,y0);
        x = y0;
        y = x0 - (a/b)*y0;
        return d;
    }
}

int Inv(int A,int Mod)
{
    int x,y;
    int d = ExtendedGCD(A,Mod,x,y);
    while (x < 0) x += Mod;
    return x;
}

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

    int A,Mod;
    fin >> A >> Mod;
    fout << Inv(A,Mod);
}