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

#include <bits/stdc++.h>
#include <cmath>
#include <string>
using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
typedef vector<ll> vll;

#define all(v) v.begin(), v.end()
#define test()                                                                 \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  while (t--)
#define fastio                                                                 \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
const int mod = 1e9 + 7;
const int N = 1e6 + 1;
vi isprime(N, 1);
vi min_div(N);

ll power(ll x, ll a) {
  if (a == 0)
    return (1);
  int z = power(x, a / 2);
  z *= z;
  if (a % 2)
    z *= x;
  return (z);
}

void sieve() {
  isprime[0] = 0;
  isprime[1] = 0;
  int num = 2;
  while (num * num < N) {
    if (isprime[num]) {
      min_div[num] = num;
      int no = num * num;
      while (no < N) {
        isprime[no] = 0;
        min_div[no] = num;
        no += num;
      }
    }
    num += 1;
  }
  while (num < N) {
    if (isprime[num])
      min_div[num] = num;
    num++;
  }
}

int main() {
  fastio sieve();
  int n;
  cin >> n;
  vi arr(n);
  vvi factors(n);
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
    int div, num = arr[i];
    while (num > 1) {
      div = min_div[num];
      factors[i].push_back(div);
      while (num % div == 0)
        num /= div;
    }
  }
  bool pairwise = true, check = true;
  map<int, int> m;
  int gc = arr[0];
  for (int x : factors[0])
    m[x] = 1;
  for (int i = 1; i < n; i++) {
    gc = __gcd(gc, arr[i]);
    if (check) {
      for (int x : factors[i]) {
        if (m.find(x) != m.end()) {
          pairwise = false;
          check = false;
          break;
        }
        m[x] = 1;
      }
    }
  }
  if (pairwise)
    cout << "pairwise coprime" << endl;
  else if (gc == 1)
    cout << "setwise coprime" << endl;
  else
    cout << "not coprime" << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <cmath>
#include <string>
using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
typedef vector<ll> vll;

#define all(v) v.begin(), v.end()
#define test()                                                                 \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  while (t--)
#define fastio                                                                 \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
const int mod = 1e9 + 7;
const int N = 1e6 + 1;
vi isprime(N, 1);
vi min_div(N);

ll power(ll x, ll a) {
  if (a == 0)
    return (1);
  int z = power(x, a / 2);
  z *= z;
  if (a % 2)
    z *= x;
  return (z);
}

void sieve() {
  isprime[0] = 0;
  isprime[1] = 0;
  int num = 2;
  while (num * num < N) {
    if (isprime[num]) {
      min_div[num] = num;
      int no = num * num;
      while (no < N) {
        isprime[no] = 0;
        min_div[no] = num;
        no += num;
      }
    }
    num += 1;
  }
}

int main() {
  fastio sieve();
  int n;
  cin >> n;
  vi arr(n);
  vvi factors(n);
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
    int div, num = arr[i];
    while (num > 1) {
      div = min_div[num];
      factors[i].push_back(div);
      while (num % div == 0)
        num /= div;
    }
  }
  bool pairwise = true;
  map<int, int> m;
  int gc = arr[0];
  for (int x : factors[0])
    m[x] = 1;
  for (int i = 1; i < n; i++) {
    gc = __gcd(gc, arr[i]);
    for (int x : factors[i]) {
      if (m.find(x) != m.end()) {
        pairwise = false;
      }
      m[x] = 1;
    }
  }
  if (pairwise)
    cout << "pairwise coprime" << endl;
  else if (gc == 1)
    cout << "setwise coprime" << endl;
  else
    cout << "not coprime" << endl;
}