Cod sursa(job #3226748)

Utilizator _andrei4567Stan Andrei _andrei4567 Data 22 aprilie 2024 18:06:18
Problema Algoritmul lui Euclid extins Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.98 kb
///ax + by = d(d = cmmdc (a, b)) (bezout)
///a = q * b + r
///b * x1 + r * y1 = d

///x = y1
///y = x1 - q * y1
///q = (a / b)

///invers modular x si n coprime xy congruent cu 1 modulo n
///xy + kn = 1
///y e inversul modular al lui x

#include <fstream>

using namespace std;

ifstream cin ("euclid3.in");
ofstream cout ("euclid3.out");

void euclid_extins (int a, int b, int &d, int &x, int &y)
{
    if (!b)
    {
        d = a;
        x = 1;
        y = 0;
        return;
    }
    int x1, y1;
    euclid_extins (b, a % b, d, x1, y1);
    x = y1;
    y = x1 - (a / b) * y1;
}

int a, b, c, t;

void solve ()
{
    cin >> a >> b >> c;
    int x, y, d;
    x = y = d = 0;
    euclid_extins (a, b, d, x, y);
    if ((c % d) != 0)
        cout << "0 0\n";
    else
        cout << x * (c / d) << ' ' << y * (c / d) << '\n';
}

int main()
{
    ios_base :: sync_with_stdio(false);
    cin.tie(0);
    for (cin >> t; t--; solve());
    return 0;
}