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

#include <bits/stdc++.h>
using namespace std;
#define PI 3.14159265358979323
#define ll long long int
#define vi vector<int>
#define vl vector<ll>
#define all(v) (v).begin(), (v).end()
#define pb push_back
#define MOD 1000000007

ll gcd(ll a, ll b) { return (b == 0LL) ? a : gcd(b, a % b); }

vl pos(1000006, 1000008);
vl fin(1000006, -1);
vl spf(1000006, 1000008);
vl v(1000006);
ll n;
vector<bool> vis(1000006, false);

void sieve() {
  for (ll i = 2; i < 1000006; i++) {
    if (!vis[i]) {
      for (ll j = i; j < 1000005; j += i) {
        if (pos[j] != 1000008)
          fin[i] = max(fin[i], pos[j]);
        spf[j] = min(spf[j], i);
        vis[j] = true;
      }
    }
  }
}

int main() {
#ifndef ONLINE_JUDGE
  // for getting input from input.txt
  freopen("input.txt", "r", stdin);
  // for writing output to output.txt
  freopen("output.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n;
  for (ll i = 0; i < n; i++) {
    cin >> v[i];
    pos[v[i]] = i;
    spf[v[i]] = v[i];
  }
  sieve();
  int f = 0;
  for (ll i = 0; i < n; i++) {
    ll cur = v[i];
    if (cur == 1)
      continue;
    while (cur > 1) {
      ll d = spf[cur];
      if (fin[spf[cur]] > i) {
        f = 1;
        break;
      }
      while (cur % d == 0 && d)
        cur = cur / d;
    }
    if (f == 1)
      break;
  }
  int ff = 0;
  if (f == 0) {
    cout << "pairwise coprime"
         << "\\n";
    ff = 1;
  }
  ll ans = v[0];
  for (ll i = 1; i < n; i++)
    ans = gcd(ans, v[i]);
  if (ans == 1 && ff == 0) {
    cout << "setwise coprime\\n";
    ff = 1;
    // return 0;
  }
  if (ff == 0)
    cout << "not coprime\\n";
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define PI 3.14159265358979323
#define ll long long int
#define vi vector<int>
#define vl vector<ll>
#define all(v) (v).begin(), (v).end()
#define pb push_back
#define MOD 1000000007

const ll mod = 1e9 + 7;

ll power(ll a, ll b) { // a^b
  ll res = 1;
  a = a % MOD;
  while (b > 0) {
    if (b & 1) {
      res = (res * a) % MOD;
      b--;
    }
    a = (a * a) % MOD;
    b >>= 1;
  }
  return res;
}

ll gcd(ll a, ll b) { return (b == 0) ? a : gcd(b, a % b); }

vl pos(1000006, 1000008);
vl fin(1000006, 0);
vl spf(1000006);
vl v(1000006);
ll n;
vector<bool> vis(1000006, false);

void sieve() {
  for (ll i = 2; i < 1000006; i++) {
    if (!vis[i]) {
      for (ll j = i; j < 1000005; j += i) {
        if (pos[j] != 1000008)
          fin[i] = max(fin[i], pos[j]);
        spf[j] = min(spf[j], i);
        vis[j] = true;
      }
    }
  }
}

int main() {
#ifndef ONLINE_JUDGE
  // for getting input from input.txt
  freopen("input.txt", "r", stdin);
  // for writing output to output.txt
  freopen("output.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n;
  for (ll i = 0; i < n; i++) {
    cin >> v[i];
    pos[v[i]] = i;
    spf[v[i]] = v[i];
  }
  sieve();
  int f = 0;
  for (ll i = 0; i < n; i++) {
    ll cur = v[i];
    if (cur == 1)
      continue;
    while (cur > 1) {
      ll d = spf[cur];
      if (fin[spf[cur]] > i) {
        f = 1;
        break;
      }
      while (cur % d == 0 && d)
        cur = cur / d;
    }
    if (f == 1)
      break;
  }
  int ff = 0;
  if (f == 0) {
    cout << "pairwise coprime"
         << "\\n";
    ff = 1;
  }
  ll ans = v[0];
  for (ll i = 1; i < n; i++)
    ans = gcd(ans, v[i]);
  if (ans == 1 && ff == 0) {
    cout << "setwise coprime\\n";
    ff = 1;
    // return 0;
  }
  if (ff == 0)
    cout << "not coprime\\n";
}