Cod sursa(job #3312539)

Utilizator depevladVlad Dumitru-Popescu depevlad Data 29 septembrie 2025 01:04:23
Problema Algoritmul lui Euclid extins Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.68 kb
#include <bits/stdc++.h>

// ax + by = d

void euclid(int a, int b, int* d, int* x, int* y) {
  if (!b) {
    *d = a;
    *x = 1;
    *y = 0;
  } else {
    int x0;
    int y0;
    euclid(b, a % b, d, &x0, &y0);
    *x = y0;
    *y = x0 - (a / b) * y0;
  }
}

int main() {
#ifndef LOCAL
  freopen("euclid3.in", "r", stdin);
  freopen("euclid3.out", "w", stdout);
#endif
  int T;
  std::cin >> T;
  for (; T--;) {
    int a;
    int b;
    int c;
    std::cin >> a >> b >> c;
    int x;
    int y;
    int d;
    euclid(a, b, &d, &x, &y);
    if (!(c % d)) {
      x *= (c / d);
      y *= (c / d);
    } else {
      x = 0;
      y = 0;
    }
    std::cout << x << " " << y << "\n";
  }
  return 0;
}