Cod sursa(job #2886864)

Utilizator toma_ariciuAriciu Toma toma_ariciu Data 8 aprilie 2022 15:00:13
Problema Invers modular Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.67 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#include <algorithm>

using namespace std;

const string filename = "inversmodular";
ifstream fin(filename + ".in");
ofstream fout(filename + ".out");

void euclid_extins(int a, int b, long long &x, long long &y)
{
    if(b == 0)
    {
        x = 1;
        y = 0;
        return;
    }
    euclid_extins(b, a % b, x, y);
    long long xprim = x, yprim = y;
    x = yprim;
    y = xprim - (a / b) * yprim;
}

int main()
{
    int a, b;
    long long x, y;
    fin >> b >> a;
    euclid_extins(a, b, x, y);
    while(y < 0)
        y += a;
    fout << y;
    return 0;
}