Cod sursa(job #3206567)

Utilizator insertoknamedemian ilie insertokname Data 23 februarie 2024 15:11:15
Problema Invers modular Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.54 kb
#include <iostream>
#include <fstream>

void euclid(int a, int b, int &d, int &x, int &y) {
    if (b == 0) {
        d = a;
        x = 1;
        y = 1;
    }
    else {
        int x1, y1;
        euclid(b, a%b, d, x1,y1);
        x = y1;
        y = x1 - a / b * y1;
    }
}

int main() {
    std::ifstream ifile("inversmodular.in");
    std::ofstream ofile("inversmodular.out");

    int a, n;
    ifile >> a >> n;

    int x, y, d;
    euclid(a, n, d, x, y);

    while (x < 0) {
        x += n;
    }
    ofile << x;
}