Cod sursa(job #943383)

Utilizator BitOneSAlexandru BitOne Data 25 aprilie 2013 10:20:41
Problema Invers modular Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.53 kb
#include <fstream>
#include <cstdlib>

using namespace std;

inline int gcd(int a, int b, int& x, int& y)
{
    if(!b)
    {
        x = 1;
        y = 0;
        return a;
    }
    int x0, y0, d;

    d = gcd(b, a % b, x0, y0);
    x = y0;
    y = x0 - (a / b) * y0;

    return d;
}

int main()
{
    int A, N, x, y;
    ifstream in("inversmodular.in");
    ofstream out("inversmodular.out");

    in >> A >> N;
    gcd(A, N, x, y);
    if(x < 0) x += N;
    out << x << '\n';

    return EXIT_SUCCESS;
}