Problem p00654 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef long long Int;
typedef vector<int> vint;
typedef pair<int, int> pint;
typedef vector<string> vstring;
typedef vector<pint> vpint;

struct Edge {
  int to, from, cost;
};

#ifdef DEBUG
#define debug cout
#else
stringstream __ss__;
#define debug __ss__
#endif

template <class T> void pv(T a, T b) {
  for (T i = a; i != b; ++i)
    debug << *i << " ";
  debug << endl;
}
template <class T> void chmin(T &t, T f) {
  if (t > f)
    t = f;
}
template <class T> void chmax(T &t, T f) {
  if (t < f)
    t = f;
}
int in() {
  int x;
  scanf("%d", &x);
  return x;
}

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repd(i, n) for (int i = (n)-1; i >= 0; i--)
#define repn(i, m, n) for (int i = (m); i <= (n); ++i)
#define repnd(i, m, n) for (int i = (n)-1; i >= (m); i--)
#define rep0(i, n) for (i = 0; i < (n); ++i)
#define all(n) n.begin(), n.end()
#define sz(n) ((int)(n).size())
#define IL for (;;)
#define MP make_pair
#define PB push_back
#define SS stringstream
#define X second
#define Y first
#define PUTLINE debug << "LINE:" << __LINE__ << endl;

const int INF = 2147483647 / 3;
const double EPS = 1e-10;
const double PI = acos(-1.0);

const int dx[] = {1, -1, 0, 0, 1, -1, 1, -1, 0};
const int dy[] = {0, 0, 1, -1, 1, -1, -1, 1, 0};

Int a[1000000];

Int gcd(Int x, Int y) {
  if (x < y)
    return gcd(y, x);
  if (y == 0)
    return x;
  return gcd(y, x % y);
}

Int sqrt(Int x, Int m, Int n) {
  if (m + 1 == n)
    return m;
  Int l = (m + n) / 2;
  if (x >= l * l)
    return sqrt(x, l, n);
  else
    return sqrt(x, m, l);
}

int main() {
  int n;
  IL {
    cin >> n;
    if (n == 0)
      break;
    n = n * (n + 1) / 2;
    rep(i, n) cin >> a[i];
    sort(a, a + n);
    Int x = -1, y = -1, z = -1;
    rep(i, n) {
      if (a[i] % 2 == 0) {
        if (x < 0)
          x = a[i];
        else if (y < 0)
          y = a[i];
      } else if (z < 0) {
        z = a[i];
      }
    }
    // cout<<x<<" "<<y<<" "<<z<<endl;
    Int g = gcd(x, z);
    x /= g;
    z /= g;
    y /= z;
    Int w = x * y;
    w = sqrt(w, 0, 1LL << 31);
    cout << w << endl;
    bool first = true;
    rep(i, n) {
      if (a[i] % 2 == 0) {
        if (!first)
          cout << " ";
        first = false;
        cout << a[i] / w;
      }
    }
    cout << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef long long Int;
typedef vector<int> vint;
typedef pair<int, int> pint;
typedef vector<string> vstring;
typedef vector<pint> vpint;

struct Edge {
  int to, from, cost;
};

#ifdef DEBUG
#define debug cout
#else
stringstream __ss__;
#define debug __ss__
#endif

template <class T> void pv(T a, T b) {
  for (T i = a; i != b; ++i)
    debug << *i << " ";
  debug << endl;
}
template <class T> void chmin(T &t, T f) {
  if (t > f)
    t = f;
}
template <class T> void chmax(T &t, T f) {
  if (t < f)
    t = f;
}
int in() {
  int x;
  scanf("%d", &x);
  return x;
}

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repd(i, n) for (int i = (n)-1; i >= 0; i--)
#define repn(i, m, n) for (int i = (m); i <= (n); ++i)
#define repnd(i, m, n) for (int i = (n)-1; i >= (m); i--)
#define rep0(i, n) for (i = 0; i < (n); ++i)
#define all(n) n.begin(), n.end()
#define sz(n) ((int)(n).size())
#define IL for (;;)
#define MP make_pair
#define PB push_back
#define SS stringstream
#define X second
#define Y first
#define PUTLINE debug << "LINE:" << __LINE__ << endl;

const int INF = 2147483647 / 3;
const double EPS = 1e-10;
const double PI = acos(-1.0);

const int dx[] = {1, -1, 0, 0, 1, -1, 1, -1, 0};
const int dy[] = {0, 0, 1, -1, 1, -1, -1, 1, 0};

Int a[300];

Int gcd(Int x, Int y) {
  if (x < y)
    return gcd(y, x);
  if (y == 0)
    return x;
  return gcd(y, x % y);
}

Int sqrt(Int x, Int m, Int n) {
  if (m + 1 == n)
    return m;
  Int l = (m + n) / 2;
  if (x >= l * l)
    return sqrt(x, l, n);
  else
    return sqrt(x, m, l);
}

int main() {
  int n;
  IL {
    cin >> n;
    if (n == 0)
      break;
    n = n * (n + 1) / 2;
    rep(i, n) cin >> a[i];
    sort(a, a + n);
    Int x = -1, y = -1, z = -1;
    rep(i, n) {
      if (a[i] % 2 == 0) {
        if (x < 0)
          x = a[i];
        else if (y < 0)
          y = a[i];
      } else if (z < 0) {
        z = a[i];
      }
    }
    // cout<<x<<" "<<y<<" "<<z<<endl;
    Int g = gcd(x, z);
    x /= g;
    z /= g;
    y /= z;
    Int w = x * y;
    w = sqrt(w, 0, 1LL << 31);
    if (w == 0)
      return 0;
    cout << w << endl;
    bool first = true;
    rep(i, n) {
      if (a[i] % 2 == 0) {
        if (!first)
          cout << " ";
        first = false;
        cout << a[i] / w;
      }
    }
    cout << endl;
  }
  return 0;
}