Cod sursa(job #3143924)

Utilizator MAlex2019Melintioi George Alexandru MAlex2019 Data 3 august 2023 11:14:29
Problema Invers modular Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.54 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 n, ll &x, ll &y) {
    if (n == 0) {
        x = 1;
        y = 0;
        return;
    }
    euclidExt(n, a%n, x, y);
    ll xp, yp;
    xp = y;
    yp = x - (a/n)*y;
    x = xp;
    y = yp;
}


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

    return 0;
}