Cod sursa(job #2939173)

Utilizator MAlex2019Melintioi George Alexandru MAlex2019 Data 13 noiembrie 2022 10:39:52
Problema Invers modular Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.57 kb
#include <iostream>
#include <fstream>

using namespace std;

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

typedef long long ll;

void euclidExt(ll a, ll b, ll &d, ll &x, ll &y) {
    if (b == 0LL) {
        d = a;
        x = 1LL;
        y = 0LL;
        return;
    }
    ll q = a/b, x2, y2;
    euclidExt(b, a - q*b, d, x2, y2);
    x = y2;
    y = x2 - q*y2;
}


int main() {
    long long a, n;
    fin >> a >> n;
    long long x, y, d = 1LL;
    euclidExt(a, n, d, x, y);
    fout << (x%n + n)%n << '\n';

    return 0;
}