Cod sursa(job #3238765)

Utilizator obsidianMidnight Majesty obsidian Data 30 iulie 2024 12:59:56
Problema Invers modular Scor 50
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.53 kb
#include <fstream>
using namespace std;

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

int gcd(int a, int b)
{
    return b == 0 ? a : gcd(b, a % b);
}

void euclid_ext(int a, int b, int &x, int &y)
{
    if (b == 0)
    {
        x = 1, y = 0;
        return;
    }
    int px, py;
    euclid_ext(b, a % b, px, py);
    x = py;
    y = px - (a / b) * py;
}

int main()
{
    int a, b, x, y;
    fin >> a >> b;
    euclid_ext(a, b, x, y);
    fout << 1 / gcd(a, b) * x;
    return 0;
}