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

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

#define ll long long int
#define pb push_back
#define f(i, x, y) for (i = x; i < y; i++)
#define vll vector<ll>
#define fast                                                                   \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define yes cout << "YES" << endl;
#define no cout << "NO" << endl;
#define mod 1000000007

vll factors(1000005, 0);

int main() {
  fast;
  ll n, i, a;
  cin >> n;

  vll v;

  f(i, 0, n) {
    cin >> a;
    v.pb(a);
  }
  ll gcd = v[0];

  f(i, 1, n) { gcd = __gcd(gcd, v[i]); }
  if (gcd != 1)
    cout << "not coprime" << endl;
  else {
    ll f = 0;
    f(i, 0, n) {
      ll num = v[i];

      ll j;
      for (j = 1; j * j <= num; j++) {
        if (num % j == 0) {
          ll f1, f2;
          f1 = j;
          f2 = num / j;

          if (f1 == f2) {
            f2 = 1;
          }

          if (f1 != 1) {
            if (factors[f1] != 0) {
              f = 1;
              break;
            } else
              factors[f1] = 1;
          }

          if (f2 != 1) {
            if (factors[f2] != 0) {
              f = 1;
              break;
            } else
              factors[f2] = 1;
          }
        }
      }
      if (f == 1)
        break;
    }
    if (f == 1)
      cout << "setwise coprime" << endl;
    else
      cout << "pairwise coprime" << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

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

#define ll long long int
#define pb push_back
#define f(i, x, y) for (i = x; i < y; i++)
#define vll vector<ll>
#define fast                                                                   \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define yes cout << "YES" << endl;
#define no cout << "NO" << endl;
#define mod 1000000007

int main() {
  fast;
  ll n, i, a;
  cin >> n;

  vll v;

  f(i, 0, n) {
    cin >> a;
    v.pb(a);
  }
  ll gcd = v[0];

  f(i, 1, n) { gcd = __gcd(gcd, v[i]); }
  if (gcd != 1)
    cout << "not coprime" << endl;
  else {

    ll f = 0, j;
    vector<ll> m(1000001, 0);

    vll prime(1000001, 0);
    f(i, 2, 1000001) {
      for (j = 2; j * i < 1000001; j++)
        prime[i * j] = 1;
    }
    vll pn;
    f(i, 2, 1000001) {
      if (prime[i] == 0)
        pn.pb(i);
    }
    /*f(i,0,pn.size())
        cout<<pn[i]<<endl;*/
    for (i = n - 1; i >= 0; i--) {
      ll num = v[i];
      // ll j=0;
      j = 0;
      while (num != 1) {
        while (num % pn[j] == 0) {
          num = num / pn[j];
          if (m[pn[j]] != 0) {
            f = 1;
            break;
          }
        }
        j++;
      }
      if (f == 1)
        break;
      else {
        num = v[i];
        j = 0;
        while (num != 1) {
          while (num % pn[j] == 0) {
            num = num / pn[j];
            if (m[pn[j]] == 0)
              m[pn[j]] = 1;
          }

          j++;
        }
      }
    }
    if (f == 1)
      cout << "setwise coprime" << endl;
    else
      cout << "pairwise coprime" << endl;
  }
  return 0;
}