Cod sursa(job #2152786)

Utilizator andra_moldovanAndra Moldovan andra_moldovan Data 5 martie 2018 19:50:05
Problema Invers modular Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.62 kb
#include <fstream>

using namespace std;

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

inline void Euclid(int a, int b, long long &x, long long &y) {
    if (b == 0) {
        x = 1;
        y = 0;
    }
    else {
        long long xx, yy;

        Euclid(b, a % b, xx, yy);
        x = yy;
        y = xx - (a / b) * yy;
    }
}

inline void Read() {
    long long inv, inb;
    int A, N;

    fin >> A >> N;

    Euclid(A, N, inv, inb);

    while (inv < 0)
        inv += N;

    fout << inv;
}

int main () {
    Read();

    fin.close(); fout.close(); return 0;
}