Problem p02574 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

#define all(X) (X).begin(), (X).end()
#define ll long long
#define linf (1ll << 60)

using namespace std;

// -------- FUNCS -------- //
vector<string> SPLIT(string str, char ch) {
  vector<string> res;
  stringstream ss(str);
  string s;
  while (getline(ss, s, ch))
    res.push_back(s);
  return res;
}
ll STOI(string str, int base) {
  ll res = 0;
  for (auto c : str) {
    res *= base;
    res += c - '0';
  }
  return res;
}
string ITOS(ll val, int base) {
  string res;
  while (val > 0) {
    res += '0' + (val % base);
    val /= base;
  }
  reverse(all(res));
  return res;
}

// Sieve of Eratosthenes
// (*this)[i] = (divisor of i, greater than 1)
// Example: [0, 1, 2, 3, 2, 5, 3, 7, 2, 3, 2, 11, ...]
// Complexity: Space O(MAXN), Time (construction) O(MAXNloglogMAXN)
struct SieveOfEratosthenes : std::vector<int> {
  std::vector<int> primes;
  SieveOfEratosthenes(int MAXN) : std::vector<int>(MAXN + 1) {
    std::iota(begin(), end(), 0);
    for (int i = 2; i <= MAXN; i++) {
      if ((*this)[i] == i) {
        primes.push_back(i);
        for (int j = i; j <= MAXN; j += i)
          (*this)[j] = i;
      }
    }
  }
  using T = long long int;
  // Prime factorization for x <= MAXN^2
  // Complexity: O(log x)          (x <= MAXN)
  //             O(MAXN / logMAXN) (MAXN < x <= MAXN^2)
  std::map<T, int> Factorize(T x) {
    assert(x <= 1LL * (int(size()) - 1) * (int(size()) - 1));
    std::map<T, int> ret;
    if (x < int(size())) {
      while (x > 1) {
        ret[(*this)[x]]++;
        x /= (*this)[x];
      }
    } else {
      for (auto p : primes) {
        while (!(x % p))
          x /= p, ret[p]++;
        if (x == 1)
          break;
      }
      if (x > 1)
        ret[x]++;
    }
    return ret;
  }
  std::vector<T> Divisors(T x) {
    std::vector<T> ret{1};
    for (auto p : Factorize(x)) {
      int n = ret.size();
      for (int i = 0; i < n; i++) {
        for (T a = 1, d = 1; d <= p.second; d++) {
          a *= p.first;
          ret.push_back(ret[i] * a);
        }
      }
    }
    return ret; // Not sorted
  }
  // Moebius function Table
  // return: [0=>0, 1=>1, 2=>-1, 3=>-1, 4=>0, 5=>-1, 6=>1, 7=>-1, 8=>0, ...]
  std::vector<int> GenerateMoebiusFunctionTable() {
    std::vector<int> ret(size());
    for (int i = 1; i < int(size()); i++) {
      if (i == 1)
        ret[i] = 1;
      else if ((i / (*this)[i]) % (*this)[i] == 0)
        ret[i] = 0;
      else
        ret[i] = -ret[i / (*this)[i]];
    }
    return ret;
  }
};

// -------- Main Code -------- //
const ll maxN = 100005;
SieveOfEratosthenes sieve(maxN);

int n;
vector<int> arr;
void runcase() {
  cin >> n;
  arr.resize(n);
  for (auto &u : arr)
    cin >> u;

  // cnt
  vector<int> cnt(maxN, 0);
  for (auto u : arr) {
    for (auto p : sieve.Factorize(u)) {
      cnt[p.first]++;
    }
  }

  // calc
  int res = 1;
  for (auto u : cnt)
    if (u >= 2)
      res = 2;
  if (res == 2)
    for (auto u : cnt)
      if (u == n)
        res = 3;

  // verdict
  if (res == 1)
    cout << "pairwise coprime\n";
  else if (res == 2)
    cout << "setwise coprime\n";
  else
    cout << "not coprime\n";

  return;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  // int t;
  // cin >> t;
  // while (t-- > 0)
  runcase();
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

#define all(X) (X).begin(), (X).end()
#define ll long long
#define linf (1ll << 60)

using namespace std;

// -------- FUNCS -------- //
vector<string> SPLIT(string str, char ch) {
  vector<string> res;
  stringstream ss(str);
  string s;
  while (getline(ss, s, ch))
    res.push_back(s);
  return res;
}
ll STOI(string str, int base) {
  ll res = 0;
  for (auto c : str) {
    res *= base;
    res += c - '0';
  }
  return res;
}
string ITOS(ll val, int base) {
  string res;
  while (val > 0) {
    res += '0' + (val % base);
    val /= base;
  }
  reverse(all(res));
  return res;
}

// Sieve of Eratosthenes
// (*this)[i] = (divisor of i, greater than 1)
// Example: [0, 1, 2, 3, 2, 5, 3, 7, 2, 3, 2, 11, ...]
// Complexity: Space O(MAXN), Time (construction) O(MAXNloglogMAXN)
struct SieveOfEratosthenes : std::vector<int> {
  std::vector<int> primes;
  SieveOfEratosthenes(int MAXN) : std::vector<int>(MAXN + 1) {
    std::iota(begin(), end(), 0);
    for (int i = 2; i <= MAXN; i++) {
      if ((*this)[i] == i) {
        primes.push_back(i);
        for (int j = i; j <= MAXN; j += i)
          (*this)[j] = i;
      }
    }
  }
  using T = long long int;
  // Prime factorization for x <= MAXN^2
  // Complexity: O(log x)          (x <= MAXN)
  //             O(MAXN / logMAXN) (MAXN < x <= MAXN^2)
  std::map<T, int> Factorize(T x) {
    assert(x <= 1LL * (int(size()) - 1) * (int(size()) - 1));
    std::map<T, int> ret;
    if (x < int(size())) {
      while (x > 1) {
        ret[(*this)[x]]++;
        x /= (*this)[x];
      }
    } else {
      for (auto p : primes) {
        while (!(x % p))
          x /= p, ret[p]++;
        if (x == 1)
          break;
      }
      if (x > 1)
        ret[x]++;
    }
    return ret;
  }
  std::vector<T> Divisors(T x) {
    std::vector<T> ret{1};
    for (auto p : Factorize(x)) {
      int n = ret.size();
      for (int i = 0; i < n; i++) {
        for (T a = 1, d = 1; d <= p.second; d++) {
          a *= p.first;
          ret.push_back(ret[i] * a);
        }
      }
    }
    return ret; // Not sorted
  }
  // Moebius function Table
  // return: [0=>0, 1=>1, 2=>-1, 3=>-1, 4=>0, 5=>-1, 6=>1, 7=>-1, 8=>0, ...]
  std::vector<int> GenerateMoebiusFunctionTable() {
    std::vector<int> ret(size());
    for (int i = 1; i < int(size()); i++) {
      if (i == 1)
        ret[i] = 1;
      else if ((i / (*this)[i]) % (*this)[i] == 0)
        ret[i] = 0;
      else
        ret[i] = -ret[i / (*this)[i]];
    }
    return ret;
  }
};

// -------- Main Code -------- //
const ll maxN = 1000005;
SieveOfEratosthenes sieve(maxN);

int n;
vector<int> arr;
void runcase() {
  cin >> n;
  arr.resize(n);
  for (auto &u : arr)
    cin >> u;

  // cnt
  vector<int> cnt(maxN, 0);
  for (auto u : arr) {
    for (auto p : sieve.Factorize(u)) {
      cnt[p.first]++;
    }
  }

  // calc
  int res = 1;
  for (auto u : cnt)
    if (u >= 2)
      res = 2;
  if (res == 2)
    for (auto u : cnt)
      if (u == n)
        res = 3;

  // verdict
  if (res == 1)
    cout << "pairwise coprime\n";
  else if (res == 2)
    cout << "setwise coprime\n";
  else
    cout << "not coprime\n";

  return;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  // int t;
  // cin >> t;
  // while (t-- > 0)
  runcase();
  return 0;
}