Cod sursa(job #2870089)

Utilizator toma_ariciuAriciu Toma toma_ariciu Data 12 martie 2022 08:26:18
Problema Invers modular Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.71 kb
#include <iostream>
#include <fstream>

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 ax = x, ay = y;
    x = ay;
    y = ax - (a / b) * ay;
}

int main()
{
    bool invers = 0;
    int a, b, c;
    long long x, y;
    fin >> a >> b;
    if(a < b)
        swap(a, b), invers = 1;
    euclid_extins(a, b, x, y);
    if(invers)
        swap(x, y), swap(a, b);
    while(x < 0)
        x += b;
    fout << x;
    return 0;
}