Cod sursa(job #2025099)

Utilizator SlevySlevoaca Stefan-Gabriel Slevy Data 21 septembrie 2017 21:03:37
Problema Invers modular Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.54 kb
#include <fstream>

using namespace std;

void gcd(int a, int b, int& x, int& y) {

    if (b == 0) {

        x = 1;
        y = 0;
    }
    else {

        gcd(b, a % b, x, y);
        int aux = y;
        y = x - (a / b) * y;
        x = aux;
    }
}

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

    gcd(A, N, x, y);

    if (x <= 0)
        x = N + x % N;

    ofstream out("inversmodular.out");
    out << x << "\n";
    out.close();

    return 0;
}