Problem p02574 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pld pair<long double, int>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pb push_back
#define ff first
#define ss second
#define SZ(x) ((int)(x).size())
#define ld long double
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const int MAXN = 1e6 + 100;
int N, a[MAXN], smallest[MAXN];
bool prime[MAXN];
string ans1 = "pairwise coprime";
string ans2 = "setwise coprime";
string ans3 = "not coprime";

void read() {
  cin >> N;
  for (int i = 0; i < N; i++)
    cin >> a[i];
}

void sieve() {
  memset(prime, true, sizeof(prime));
  prime[0] = prime[1] = false;
  smallest[0] = 0, smallest[1] = 1;
  for (int i = 2; i * i < MAXN; i++) {
    if (prime[i]) {
      smallest[i] = i;
      for (int j = 2 * i; j < MAXN; j += i)
        prime[j] = false, smallest[j] = i;
    }
  }
}

void solve() {
  read();
  sieve();
  vector<int> cnt(MAXN, 0);
  int d = a[0];
  for (int i = 0; i < N; i++) {
    d = __gcd(d, a[i]);
    int n = a[i];
    while (n != 1) {
      cnt[smallest[n]]++;
      n /= smallest[n];
    }
  }
  bool a2 = (d == 1);
  for (int i = 0; i < N; i++) {
    int n = a[i];
    while (n != 1) {
      int cnt2 = 0, div = smallest[n];
      while (n != 1 && (n % div == 0)) {
        n /= div;
        cnt2++;
      }
      if (cnt[div] > cnt2) {
        if (a2 == true)
          cout << ans2;
        else
          cout << ans3;
        return;
      }
    }
  }
  cout << ans1;
  return;
}

int main() {
  ios_base::sync_with_stdio(false);
  solve();
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pld pair<long double, int>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pb push_back
#define ff first
#define ss second
#define SZ(x) ((int)(x).size())
#define ld long double
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const int MAXN = 1e6 + 100;
int N, a[MAXN], smallest[MAXN];
bool prime[MAXN];
string ans1 = "pairwise coprime";
string ans2 = "setwise coprime";
string ans3 = "not coprime";

void read() {
  cin >> N;
  for (int i = 0; i < N; i++)
    cin >> a[i];
}

void sieve() {
  memset(prime, true, sizeof(prime));
  prime[0] = prime[1] = false;
  iota(smallest, smallest + MAXN, 0);
  for (int i = 2; i * i < MAXN; i++) {
    if (prime[i]) {
      smallest[i] = i;
      for (int j = 2 * i; j < MAXN; j += i)
        prime[j] = false, smallest[j] = i;
    }
  }
}

void solve() {
  read();
  sieve();
  vector<int> cnt(MAXN, 0);
  int d = a[0];
  for (int i = 0; i < N; i++) {
    d = __gcd(d, a[i]);
    int n = a[i];
    while (n != 1) {
      cnt[smallest[n]]++;
      n /= smallest[n];
    }
  }
  bool a2 = (d == 1);
  for (int i = 0; i < N; i++) {
    int n = a[i];
    while (n != 1) {
      int cnt2 = 0, div = smallest[n];
      while (n != 1 && (n % div == 0)) {
        n /= div;
        cnt2++;
      }
      if (cnt[div] > cnt2) {
        if (a2 == true)
          cout << ans2;
        else
          cout << ans3;
        return;
      }
    }
  }
  cout << ans1;
  return;
}

int main() {
  ios_base::sync_with_stdio(false);
  solve();
  return 0;
}