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

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

using Array = vector<double>;
using Matrix = vector<Array>;
const double eps = 1e-10;

Array GaussJordan(const Matrix &A, const Array &b) {
  int n = A.size();
  Matrix B(n, Array(n + 1));
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      B[i][j] = A[i][j];
  for (int i = 0; i < n; i++)
    B[i][n] = b[i];
  for (int i = 0; i < n; i++) {
    int pivot = i;
    for (int j = i; j < n; j++) {
      if (abs(B[i][j]) > abs(B[pivot][i]))
        pivot = i;
    }
    swap(B[i], B[pivot]);
    if (abs(B[i][i]) < eps)
      return Array();
    for (int j = i + 1; j <= n; j++)
      B[i][j] /= B[i][i];
    for (int j = 0; j < n; j++) {
      if (i != j) {
        for (int k = i + 1; k <= n; k++)
          B[j][k] -= B[j][i] * B[i][k];
      }
    }
  }

  Array x(n);
  for (int i = 0; i < n; i++)
    x[i] = B[i][n];
  return x;
}

double a[10];

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int d;
  while (cin >> d, d) {
    for (int i = 0; i < d + 3; i++) {
      cin >> a[i];
    }

    vector<int> ans;
    for (int i = 0; i < d + 3; i++) {
      Array y;
      bool flag = true;
      for (int j = 0; j < d + 3; j++) {
        if (i == j)
          continue;
        Matrix A;
        Array b;
        for (int x = 0; x < d + 3; x++) {
          if (i == x || j == x)
            continue;
          Array arr(d + 1);
          for (int k = 0; k <= d; k++) {
            arr[k] = pow(x, k);
          }
          A.push_back(arr);
          b.push_back(a[x]);
        }

        Array r = GaussJordan(A, b);
        if (r.size()) {
          if (y.size() == 0)
            y = r;
          else {
            for (int k = 0; k < r.size(); k++) {
              if (abs(r[k] - y[k]) > 1e-1) {
                flag = false;
                break;
              }
            }
          }
        } else {
          flag = false;
        }
        if (!flag)
          break;
      }
      if (flag)
        ans.push_back(i);
    }

    cout << ans[0] << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

using Array = vector<double>;
using Matrix = vector<Array>;
const double eps = 1e-10;

Array GaussJordan(const Matrix &A, const Array &b) {
  int n = A.size();
  Matrix B(n, Array(n + 1));
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      B[i][j] = A[i][j];
  for (int i = 0; i < n; i++)
    B[i][n] = b[i];
  for (int i = 0; i < n; i++) {
    int pivot = i;
    for (int j = i; j < n; j++) {
      if (abs(B[i][j]) > abs(B[pivot][i]))
        pivot = i;
    }
    swap(B[i], B[pivot]);
    if (abs(B[i][i]) < eps)
      return Array();
    for (int j = i + 1; j <= n; j++)
      B[i][j] /= B[i][i];
    for (int j = 0; j < n; j++) {
      if (i != j) {
        for (int k = i + 1; k <= n; k++)
          B[j][k] -= B[j][i] * B[i][k];
      }
    }
  }

  Array x(n);
  for (int i = 0; i < n; i++)
    x[i] = B[i][n];
  return x;
}

double a[10];

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int d;
  while (cin >> d, d) {
    for (int i = 0; i < d + 3; i++) {
      cin >> a[i];
    }

    vector<int> ans;
    for (int i = 0; i < d + 3; i++) {
      Array y;
      bool flag = true;
      for (int j = 0; j < d + 3; j++) {
        if (i == j)
          continue;
        Matrix A;
        Array b;
        for (int x = 0; x < d + 3; x++) {
          if (i == x || j == x)
            continue;
          Array arr(d + 1);
          for (int k = 0; k <= d; k++) {
            arr[k] = pow(x, k);
          }
          A.push_back(arr);
          b.push_back(a[x]);
        }

        Array r = GaussJordan(A, b);
        if (r.size()) {
          if (y.size() == 0)
            y = r;
          else {
            for (int k = 0; k < r.size(); k++) {
              if (abs(r[k] - y[k]) > 1e-4) {
                flag = false;
                break;
              }
            }
          }
        } else {
          flag = false;
        }
        if (!flag)
          break;
      }
      if (flag)
        ans.push_back(i);
    }

    cout << ans[0] << endl;
  }
}