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

#include <algorithm>
#include <cmath>
#include <ctime>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using pii = pair<int, int>;
using ppi = pair<pii, int>;

ll gcd(ll a, ll b) {
  if (b > a) {
    ll c = a;
    a = b;
    b = c;
  }
  while (b != 0) {
    ll c = b;
    b = a % b;
    a = c;
  }
  return a;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int ti = clock();
  // start-----------------------------------------------
  ll n;
  cin >> n;
  vector<ll> a(n);
  vector<ll> divisor(1000000 + 1, 0);
  vector<ll> primes;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i <= 1000000; i++) {
    divisor[i] = i;
  }
  for (int i = 2; i <= 1000000; i++) {
    if (divisor[i] == i) {
      primes.push_back(i);
      int t = 2;
      while (i * t <= 1000000) {
        if (divisor[i * t] > i)
          divisor[i * t] = i;
        t++;
      }
    }
  }
  vector<int> used(1000000 + 1, 0);
  for (int i = 0; i < n; i++)
    used[a[i]]++;
  for (auto p : primes) {
    int cnt = 0;
    for (int i = p; i <= 1000000; i += p) {
      cnt += used[i];
    }
    if (cnt > 1) {
      ll t = a[0];
      for (int i = 1; i < n; i++) {
        t = gcd(t, a[i]);
      }
      if (t > 1) {
        cout << "not coprime" << endl;
        return 0;
      } else {
        cout << "setwise coprime" << endl;
        return 0;
      }
    }
  }
  cout << "pairwise coprime" << endl;
  return 0;

  // end-----------------------------------------------
  // cerr << 1.0 * (clock() - ti) / CLOCKS_PER_SEC << endl;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <ctime>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using pii = pair<int, int>;
using ppi = pair<pii, int>;

ll gcd(ll a, ll b) {
  if (b > a) {
    ll c = a;
    a = b;
    b = c;
  }
  while (b != 0) {
    ll c = b;
    b = a % b;
    a = c;
  }
  return a;
}

vector<ll> divisor(1000000 + 1, 0);

void calc(ll a, vector<int> &used) {
  vi tmp(1000001, 0);
  while (a > 1) {
    if (tmp[divisor[a]] == 0)
      used[divisor[a]]++, tmp[divisor[a]]++;
    a /= divisor[a];
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int ti = clock();
  // start-----------------------------------------------
  ll n;
  cin >> n;
  vector<ll> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < 1000000; i++) {
    divisor[i] = i;
  }
  for (int i = 2; i <= 1000000; i++) {
    if (divisor[i] == i) {
      int t = 2;
      while (i * t <= 1000000) {
        if (divisor[i * t] > i)
          divisor[i * t] = i;
        t++;
      }
    }
  }
  vector<int> used(1000000 + 1, 0);
  for (int i = 0; i < n; i++) {
    calc(a[i], used);
  }
  for (int i = 2; i < 1000000; i++) {
    if (used[i] > 1) {
      ll t = a[0];
      for (int i = 1; i < n; i++) {
        t = gcd(t, a[i]);
      }
      if (t > 1) {
        cout << "not coprime" << endl;
        return 0;
      } else {
        cout << "setwise coprime" << endl;
        return 0;
      }
    }
  }

  cout << "pairwise coprime" << endl;
  return 0;
  // end-----------------------------------------------
  // cerr << 1.0 * (clock() - ti) / CLOCKS_PER_SEC << endl;
}