Problem p02574 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long int
#define mp make_pair
#define F first
#define S second
#define pb push_back
#define rep(i, a, b) for (ll i = a; i <= b; i++)
#define all(a) a.begin(), a.end()
#define Nmax 1000005
#define INF 1000000000
#define MOD 1000000007
#define MAXN 1000000

using namespace std;

ll mod = 1e9 + 7;

ll expo(ll base, ll exponent, ll mod) {
  ll ans = 1;
  while (exponent != 0) {
    if (exponent & 1)
      ans = (1LL * ans * base) % mod;
    base = (1LL * base * base) % mod;
    exponent >>= 1;
  }
  return ans % mod;
}

ll expo2(ll base, ll exponent) {
  ll ans = 1;
  while (exponent != 0) {
    if (exponent & 1)
      ans = (1LL * ans * base);
    base = (1LL * base * base);
    exponent >>= 1;
  }
  return ans;
}

// vector<bool>  prime(90000002,true);
// void Sieve()
// {

//     for (int p=2; p*p<=90000001; p++)
//     {
//         // If prime[p] is not changed, then it is a prime
//         if (prime[p] == true)
//         {
//             for (int i=p*p; i<=90000001; i += p)
//                 prime[i] = false;
//         }
//     }
// }

// stores smallest prime factor for every number
ll spf[MAXN];

// Calculating SPF (Smallest Prime Factor) for every
// number till MAXN.
// Time Complexity : O(nloglogn)
void sieve() {
  spf[1] = 1;
  for (ll i = 2; i < MAXN; i++)

    // marking smallest prime factor for every
    // number to be itself.
    spf[i] = i;

  // separately marking spf for every even
  // number as 2
  for (ll i = 4; i < MAXN; i += 2)
    spf[i] = 2;

  for (ll i = 3; i * i < MAXN; i++) {
    // checking if i is prime
    if (spf[i] == i) {
      // marking SPF for all numbers divisible by i
      for (ll j = i * i; j < MAXN; j += i)

        // marking spf[j] if it is not
        // previously marked
        if (spf[j] == j)
          spf[j] = i;
    }
  }
}

vector<ll> vis(1000001, 0);

int main() {

  // #ifndef ONLINE_JUDGE
  //     freopen ("data.in","r",stdin);
  //     freopen ("E.out","w",stdout);
  // #endif
  // ios_base::sync_with_stdio(false);
  // cin.tie(NULL);
  // Sieve();
  sieve();
  int tests = 1;
  // cin>>tests;
  while (tests--) {
    ll i, j, n;
    cin >> n;
    vector<ll> a(n);
    rep(i, 0, n - 1) cin >> a[i];
    ll g = __gcd(a[0], a[1]);
    for (i = 2; i < n; i++)
      g = __gcd(g, a[i]);

    bool flag = true;

    rep(i, 0, n - 1) {
      set<ll> st;
      ll tmp = a[i];
      while (tmp > 1) {
        st.insert(spf[tmp]);
        tmp /= spf[tmp];
      }
      for (auto x : st) {
        if (vis[x]) {
          flag = false;
          break;
        } else
          vis[x] = 1;
      }
      if (!flag)
        break;
    }

    if (flag)
      cout << "pairwise coprime";
    else if (g == 1)
      cout << "setwise coprime";
    else
      cout << "not coprime";
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long int
#define mp make_pair
#define F first
#define S second
#define pb push_back
#define rep(i, a, b) for (ll i = a; i <= b; i++)
#define all(a) a.begin(), a.end()
#define Nmax 1000005
#define INF 1000000000
#define MOD 1000000007
#define MAXN 1000005

using namespace std;

ll mod = 1e9 + 7;

ll expo(ll base, ll exponent, ll mod) {
  ll ans = 1;
  while (exponent != 0) {
    if (exponent & 1)
      ans = (1LL * ans * base) % mod;
    base = (1LL * base * base) % mod;
    exponent >>= 1;
  }
  return ans % mod;
}

ll expo2(ll base, ll exponent) {
  ll ans = 1;
  while (exponent != 0) {
    if (exponent & 1)
      ans = (1LL * ans * base);
    base = (1LL * base * base);
    exponent >>= 1;
  }
  return ans;
}

// vector<bool>  prime(90000002,true);
// void Sieve()
// {

//     for (int p=2; p*p<=90000001; p++)
//     {
//         // If prime[p] is not changed, then it is a prime
//         if (prime[p] == true)
//         {
//             for (int i=p*p; i<=90000001; i += p)
//                 prime[i] = false;
//         }
//     }
// }

// stores smallest prime factor for every number
ll spf[MAXN];

// Calculating SPF (Smallest Prime Factor) for every
// number till MAXN.
// Time Complexity : O(nloglogn)
void sieve() {
  spf[1] = 1;
  for (ll i = 2; i < MAXN; i++)

    // marking smallest prime factor for every
    // number to be itself.
    spf[i] = i;

  // separately marking spf for every even
  // number as 2
  for (ll i = 4; i < MAXN; i += 2)
    spf[i] = 2;

  for (ll i = 3; i * i < MAXN; i++) {
    // checking if i is prime
    if (spf[i] == i) {
      // marking SPF for all numbers divisible by i
      for (ll j = i * i; j < MAXN; j += i)

        // marking spf[j] if it is not
        // previously marked
        if (spf[j] == j)
          spf[j] = i;
    }
  }
}

vector<ll> vis(1000001, 0);

int main() {

  // #ifndef ONLINE_JUDGE
  //     freopen ("data.in","r",stdin);
  //     freopen ("E.out","w",stdout);
  // #endif
  // ios_base::sync_with_stdio(false);
  // cin.tie(NULL);
  // Sieve();
  sieve();
  int tests = 1;
  // cin>>tests;
  while (tests--) {
    ll i, j, n;
    cin >> n;
    vector<ll> a(n);
    rep(i, 0, n - 1) cin >> a[i];
    ll g = __gcd(a[0], a[1]);
    for (i = 2; i < n; i++)
      g = __gcd(g, a[i]);

    bool flag = true;

    rep(i, 0, n - 1) {
      set<ll> st;
      ll tmp = a[i];
      while (tmp > 1) {
        st.insert(spf[tmp]);
        tmp /= spf[tmp];
      }
      for (auto x : st) {
        if (vis[x]) {
          flag = false;
          break;
        } else
          vis[x] = 1;
      }
      if (!flag)
        break;
    }

    if (flag)
      cout << "pairwise coprime";
    else if (g == 1)
      cout << "setwise coprime";
    else
      cout << "not coprime";
  }
  return 0;
}