Cod sursa(job #3376953)

Utilizator FunnyStockyMihnea Andreescu FunnyStocky Data 20 noiembrie 2026 17:31:18
Problema Algoritmul lui Euclid extins Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.98 kb
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

struct Euclid {
    static ll gcd(ll a, ll b, ll &x, ll &y) {
        if (!b) { x = 1; y = 0; return a; }
        ll x1, y1;
        ll d = gcd(b, a % b, x1, y1);
        x = y1;
        y = x1 - (a / b) * y1;
        return d;
    }
    // a * x + b * y = c
    static pair<ll, ll> solve(ll a, ll b, ll c) {
        ll x, y, d = gcd(abs(a), abs(b), x, y);
        if (!d || c % d) return {0, 0};
        if (a < 0) x = -x;
        x *= c / d;
        if (b) {
            ll step = abs(b) / d;
            x = (x % step + step) % step;
            if (x > step / 2) x -= step;
            y = (c - a * x) / b;
        } else y = 0;
        return {x, y};
    }
};

int main() {
    ifstream fin("euclid3.in");
    ofstream fout("euclid3.out");

    int t;
    fin >> t;
    while (t--) {
        ll a, b, c;
        fin >> a >> b >> c;
        auto [x, y] = Euclid::solve(a, b, c);
        fout << x << ' ' << y << '\n';
    }
}