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

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define FORit(it, a) for (auto it = a.begin(); it != a.end(); it++)
#define ROF(i, a, b) for (int i = a; i >= b; i--)
#define vec vector
#define pb push_back
#define pop pop_back
#define all(x) x.begin(), x.end()
#define testcases                                                              \\
  ll t;                                                                        \\
  cin >> t;                                                                    \\
  while (t--)
#define mem(a, k) memset(a, k, sizeof(a))
#define F first
#define S second
#define MP(x, y) make_pair(x, y)
#define rt return
#define br break
#define ct continue
#define elif else if
#define ii pair<ll, ll>
#define arrin(a, n, index)                                                     \\
  for (int i = index; i < n; i++)                                              \\
  cin >> a[i]
#define vecin(a, n, index)                                                     \\
  for (int i = index; i < n; i++)                                              \\
  cin >> a[i]
#define arrout(a, n, index)                                                    \\
  for (int i = index; i < n; i++)                                              \\
    cout << a[i] << " ";                                                       \\
  cout << endl;
#define vecout(a, n, index)                                                    \\
  for (int i = index; i < n; i++)                                              \\
    cout << a[i] << " ";                                                       \\
  cout << endl;
// ll mod = 1000000007;
int s[1000001] = {0};
int check[1000001] = {0};
void sieve() {
  int maxn = 1000000;
  for (int i = 1; i <= maxn; i++)
    s[i] = -1;
  for (int i = 2; i <= maxn; i++) {
    if (s[i] == -1) {
      for (int j = i; j <= maxn; j += i) {
        if (s[j] == -1)
          s[j] = i;
      }
    }
  }
}
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  // START FROM HERE :)
  sieve();
  int n;
  cin >> n;
  int a[n];
  arrin(a, n, 0);
  int setg = a[0], flag = 1;
  // bitset <1000001> f[n];
  FOR(i, 1, n) setg = __gcd(setg, a[i]);
  FOR(i, 0, n) {
    vec<int> v;
    while (a[i] != 1) {
      if (binary_search(all(v), s[a[i]]) == false)
        v.pb(s[a[i]]);
      a[i] /= s[a[i]];
    } // gcd =1 means no common factor -> start from 1th element store its
      // factors and visit or check[fact]=1
    // now move forward and find factors if check[fact]is already one means they
    // share common factor-> flag =0 br; else mark that fact as check[fact]=1;
    FOR(k, 0, v.size()) {
      if (check[v[k]] == 0)
        check[v[k]] = 1;
      else
        flag = 0;
    }
    if (flag == 0)
      br;
  }
  if (flag == 1)
    cout << "pairwise coprime\\n";
  elif (flag == 0 && setg == 1) cout << "setwise coprime\\n";
  else cout << "not coprime\\n";
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define FORit(it, a) for (auto it = a.begin(); it != a.end(); it++)
#define ROF(i, a, b) for (int i = a; i >= b; i--)
#define vec vector
#define pb push_back
#define pop pop_back
#define all(x) x.begin(), x.end()
#define testcases                                                              \\
  ll t;                                                                        \\
  cin >> t;                                                                    \\
  while (t--)
#define mem(a, k) memset(a, k, sizeof(a))
#define F first
#define S second
#define MP(x, y) make_pair(x, y)
#define rt return
#define br break
#define ct continue
#define elif else if
#define ii pair<ll, ll>
#define arrin(a, n, index)                                                     \\
  for (int i = index; i < n; i++)                                              \\
  cin >> a[i]
#define vecin(a, n, index)                                                     \\
  for (int i = index; i < n; i++)                                              \\
  cin >> a[i]
#define arrout(a, n, index)                                                    \\
  for (int i = index; i < n; i++)                                              \\
    cout << a[i] << " ";                                                       \\
  cout << endl;
#define vecout(a, n, index)                                                    \\
  for (int i = index; i < n; i++)                                              \\
    cout << a[i] << " ";                                                       \\
  cout << endl;
// ll mod = 1000000007;
int s[1000001] = {0};
void sieve() {
  int maxn = 1000000;
  for (int i = 1; i <= maxn; i++)
    s[i] = -1;
  for (int i = 2; i <= maxn; i++) {
    if (s[i] == -1) {
      for (int j = i; j <= maxn; j += i) {
        if (s[j] == -1)
          s[j] = i;
      }
    }
  }
}
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  // START FROM HERE :)
  sieve();
  int n;
  cin >> n;
  int a[n];
  arrin(a, n, 0);
  int setg = a[0], flag = 1;
  bitset<1000001> f[n];
  bitset<1000001> test;
  FOR(i, 1, n) setg = __gcd(setg, a[i]);
  FOR(i, 0, n) {
    while (a[i] != 1) {
      f[i][s[a[i]]] = 1;
      a[i] /= s[a[i]];
    }
  }
  FOR(i, 0, n - 1) {
    FOR(j, i + 1, n) {
      if ((f[i] & f[j]) != test) {
        flag = 0;
        br;
      }
    }
    if (flag == 0)
      br;
  }
  if (flag == 1)
    cout << "pairwise coprime\\n";
  elif (flag == 0 && setg == 1) cout << "setwise coprime\\n";
  else cout << "not coprime\\n";
}