Cod sursa(job #3125628)

Utilizator TheRomulusIvan Remus TheRomulus Data 3 mai 2023 21:46:04
Problema Invers modular Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.74 kb
#include <stdio.h>
#include <fstream>

#define ll long long

using namespace std;

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

void recursiveGCD(ll& x, ll& y, int a, int b)
{
    if (!b)
        x = 1, y = 0;
    else
    {
        recursiveGCD(x, y, b, a % b);
        ll aux = x;
        x = y;
        y = aux - y * (a / b);
    }
}

ll computeModularInverse(ll A,ll M) {
    ll inv = 0, ins;

    recursiveGCD(inv, ins, A, M);

    if (inv <= 0)
        inv = M + inv % M;

    return inv;
}

int main()
{
    ios_base::sync_with_stdio(false);

    ll A, M;

    fin >> A >> M;
    
    ll modularInverse = computeModularInverse(A, M);

    fout << modularInverse;

    return 0;
}