Cod sursa(job #2922465)

Utilizator AffectiveSmile2Mihnea Matea AffectiveSmile2 Data 8 septembrie 2022 16:24:12
Problema Invers modular Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.66 kb
#include <iostream>
#include <fstream>
using namespace std;
int n;
ifstream f("inversmodular.in");
ofstream g("inversmodular.out");
void EuclidExtins(int a, int b, int &d, int &x, int &y) ///Varianta recursiva
{
    if(b == 0)
    {
        x = 1, y = 0, d = a;
    }
    else
    {
        int x0, y0;
        EuclidExtins(b, a % b, d, x0, y0);
        x = y0;
        y = x0 - (a / b) * y0;
    }
}
int inversModular2(int a)
{
    int d, x, y;
    EuclidExtins(a, n, d, x, y);
    x %= n;
    if(x < 0) x += n;
    ///while(x < 0) x += n;
    return x;
}
int main()
{
    int x;
    f>>x>>n;
    g << inversModular2(x) << endl;


}