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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

const int MX = 1e6 + 5;

int gcd(int a, int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

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

  int n;
  cin >> n;
  vector<int> a(n);
  vector<int> cnt(MX);

  int tot_gcd = 0;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    tot_gcd = (i == 0 ? a[i] : gcd(tot_gcd, a[i]));
    cnt[a[i]]++;
  }

  vector<bool> is_prime(MX, true);
  is_prime[0] = is_prime[1] = false;
  for (int i = 2; i < MX; ++i) {
    if (!is_prime[i])
      continue;
    for (int j = 2 * i; j < MX; j += i) {
      is_prime[j] = false;
    }
  }
  vector<int> primes;
  primes.reserve(MX); // stricter upper bound can be fixed
  for (int i = 0; i < MX; ++i) {
    if (is_prime[i])
      primes.push_back(i);
  }

  bool coprimePairFound = false;
  for (int prime : primes) {
    int p_cnt = 0;
    for (int multiple = prime; multiple < MX && p_cnt < 2; multiple += prime) {
      p_cnt += cnt[multiple];
    }
    if (p_cnt >= 2) {
      coprimePairFound = true;
      break;
    }
  }

  if (!coprimePairFound) {
    cout << "pairwise coprime\\n";
  } else if (tot_gcd == 1) {
    cout << "setwise coprime\\n";
  } else
    cout << "not coprime\\n";
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

template <typename A, typename B> string to_string(pair<A, B> p);

template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p);

template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p);

string to_string(const string &s) { return '"' + s + '"'; }

string to_string(const char &c) {
  string res;
  return "'" + res + c + "'";
}

string to_string(bool b) { return (b ? "True" : "False"); }

string to_string(vector<bool> v) {
  bool first = true;
  string res = "{";
  for (int i = 0; i < static_cast<int>(v.size()); i++) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(v[i]);
  }
  res += "}";
  return res;
}

template <size_t N> string to_string(bitset<N> v) {
  string res = "";
  for (size_t i = 0; i < N; i++) {
    res += static_cast<char>('0' + v[i]);
  }
  return res;
}

template <typename A> string to_string(A v) {
  bool first = true;
  string res = "{";
  for (const auto &x : v) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(x);
  }
  res += "}";
  return res;
}

template <typename A, typename B> string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}

template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " +
         to_string(get<2>(p)) + ")";
}

template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " +
         to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";
}

void debug_out() { cerr << endl; }

template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}

#ifdef LOCAL
#define debug(...) cerr << "\\t[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...)
#endif

// THINK: INT vs LONG LONG?
// for each index, store product of elements to the right (ll)
// vis[i] maps prime to whether it is visited at index i

ll gcd(ll a, ll b) {
  if (b == (ll)0)
    return a;
  else
    return gcd(b, a % b);
}

const int ub = 1e6;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);

  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  vector<bool> prime(ub + 1, true);
  prime[0] = prime[1] = 0;
  for (int i = 2; i <= ub; ++i) {
    if (!prime[i])
      continue;
    for (int j = 2 * i; j <= ub; j += i)
      prime[j] = 0;
  }
  vector<int> primes;
  primes.reserve(n);
  for (int i = 0; i <= ub; ++i) {
    if (prime[i])
      primes.push_back(i);
  }

  // vis[i][j] is true if prime[j] is found
  int num_primes = primes.size();
  vector<vector<bool>> vis(n, vector<bool>(num_primes));
  for (int i = n - 2; i >= 0; --i) {
    vis[i] = vis[i + 1];
    for (int j = 0; j < num_primes; ++j) {
      if (a[i + 1] % primes[j] == 0)
        vis[i][j] = 1;
    }
  }

  bool bad = 0;
  for (int i = 0; i < n - 1 && !bad; ++i) {
    for (int j = 0; j < num_primes; ++j) {
      if (a[i] % primes[j] == 0 && vis[i][j]) {
        bad = 1;
        break;
      }
    }
  }
  if (!bad) {
    cout << "pairwise coprime" << '\\n';
    return 0;
  }

  int tot_g = a[0];
  for (int i = 1; i < n; ++i)
    tot_g = (int)gcd(tot_g, a[i]);
  if (tot_g == 1) {
    cout << "setwise coprime" << '\\n';
    return 0;
  }

  cout << "not coprime" << '\\n';
  return 0;
}