Cod sursa(job #1534987)

Utilizator pickleVictor Andrei pickle Data 24 noiembrie 2015 10:02:54
Problema Algoritmul lui Euclid extins Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.83 kb
#include <iostream>
#include <fstream>
#include <algorithm>

using namespace std;


int x0, y0, d;
void gcd(int a, int b) { // a > b
  if (b == 0) {
    d = a;
    x0 = 1;
    y0 = 0;
    return;
  } else {
    int q = a/ b;
    gcd(b, a%b);
    int tmp = y0;
    y0 = x0 - q*y0;
    x0 = tmp;
    return;
  }
}

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

  int T, a,b, apos, bpos, c;
  fin >> T;
  for (int i = 0; i < T; i++) {
    fin >> a >> b >> c;
    apos = abs(a);
    bpos = abs(b);
    if (apos > bpos) {
      gcd(apos, bpos);
    } else {
      gcd(bpos, apos);
      swap(x0, y0);
    }
    cout << x0 << ' ' << y0 << ' ' << d << '\n';
    x0 *= a/ apos;
    y0 *= b/ bpos;
    if (c % d == 0) {
      fout << x0*(c/d) << ' ' << y0*(c/d) << '\n';
    } else {
      fout << "0 0\n";
    }
  }

  return 0;
}