Cod sursa(job #3357255)

Utilizator Andrei_GhiocelAndrei Tiberiu Ghiocel Andrei_Ghiocel Data 7 iunie 2026 18:11:56
Problema Invers modular Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.69 kb
#include <iostream>
#include <fstream>

using namespace std;

//a*x+b*y=cmmmd(a,b)
long long ext_euclid(long long a, long long b, long long& x, long long& y) {
    if (b == 0) {
        x = 1;
        y = 0;
        return a;
    }
    long long x1, y1;
    long long d = ext_euclid(b, a % b, x1, y1);
    x = y1;
    y = x1 - (a / b) * y1;
    return d;
}

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

    long long A, N;
    if (fin >> A >> N) {
        long long x, y;
        ext_euclid(A, N, x, y);

        long long invers_modular = (x % N + N) % N;

        fout << invers_modular << "\n";
    }

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