Cod sursa(job #3316488)

Utilizator forfunForfun forfun Data 18 octombrie 2025 22:25:14
Problema Algoritmul lui Euclid Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.66 kb
#include <bits/stdc++.h>
using namespace std;

// !!!
string PREFIX = "cmmdc";

int get_gcd(int x, int y) {
  if (x == 0) return y;
  if (y == 0) return x;

  int common = 1;
  while (x % 2 == 0 && y % 2 == 0) {
    common <<= 1;
    x >>= 1;
    y >>= 1;
  }
  while (x % 2 == 0) {
    x >>= 1;
  }
  while (y % 2 == 0) {
    y >>= 1;
  }
  // acum ambele numere sunt impare.
  if (x < y) swap(x, y);
  return common * get_gcd(x - y, y);
}

int main()
{
  ifstream cin(PREFIX + ".in");
  ofstream cout(PREFIX + ".out");

  int t;
  cin >> t;
  while(t--) {
    int x, y;
    cin >> x >> y;
    cout << get_gcd(x, y) << '\n';
  }

}