Cod sursa(job #3287993)

Utilizator stefangr2008Grecu Stefan stefangr2008 Data 20 martie 2025 11:28:49
Problema Invers modular Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.68 kb
#include <iostream>
#include <fstream>

using namespace std;
///InversModular(infoarena);

ifstream f("inversmodular.in");
ofstream g("inversmodular.out");

void EuclidExtins(int a, int b, int &d, int &x, int &y)
{
    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 inversMod(int A, int N)
{
    int d, x, y;
    EuclidExtins(A, N, d, x, y);
    while(x < 0)
        x += N;
    return x;
}

int main()
{
    int A, N;
    g << inversMod(A, N);
    f.close();
    g.close();
    return 0;
}