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

#include <bits/stdc++.h>
using namespace std;
#define endl '\\n'
#define fastIO                                                                 \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define int long long int
#define pb push_back
#define ff first
#define ss second
#define all(v) (v).begin(), (v).end()
#define mod (int)(998244353)
#define PI 3.14159265358979323846264338327950L
// ------------------ Debugging ------------------
#define trace(args...)                                                         \\
  {                                                                            \\
    string _s = #args;                                                         \\
    replace(_s.begin(), _s.end(), ',', ' ');                                   \\
    stringstream _ss(_s);                                                      \\
    istream_iterator<string> _it(_ss);                                         \\
    err(_it, args);                                                            \\
    cout << endl;                                                              \\
  }
void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cerr << "[ " << *it << " = " << a << " ] ";
  err(++it, args...);
}
template <typename Tk, typename Tv>
ostream &operator<<(ostream &os, const pair<Tk, Tv> &p) {
  os << "{" << p.first << ',' << p.second << "}";
  return os;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &p) {
  os << "[ ";
  for (T x : p)
    os << x << " ";
  os << "]" << endl;
  return os;
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &p) {
  os << "{ ";
  for (T x : p)
    os << x << " ";
  os << "}" << endl;
  return os;
}
template <typename Tk, typename Tv>
ostream &operator<<(ostream &os, const map<Tk, Tv> &p) {
  os << "{ ";
  for (pair<Tk, Tv> x : p)
    os << x << " ";
  os << "}" << endl;
  return os;
}
// -----------------------------------------------
int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
int lcm(int a, int b) { return (a * b) / gcd(a, b); }
const int MAXN = 1e6 + 5;
int spf[MAXN];

// Calculating SPF (Smallest Prime Factor) for every
// number till MAXN.
// Time Complexity : O(nloglogn)
void sieve() {
  spf[1] = 1;
  for (int 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 (int i = 4; i < MAXN; i += 2)
    spf[i] = 2;

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

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

// A O(log n) function returning primefactorization
// by dividing by smallest prime factor at every step
set<int> getFactorization(int x) {
  set<int> ret;
  while (x != 1) {
    ret.insert(spf[x]);
    x = x / spf[x];
  }
  return ret;
}
signed main() {

  fastIO

      int t = 1;
  // cin >> t;
  while (t--) {
    int n;
    cin >> n;
    sieve();
    vector<int> a(n);
    cin >> a[0];
    int allgcd = a[0];
    for (int i = 1; i < n; i++) {
      cin >> a[i];
      allgcd = gcd(allgcd, a[i]);
    }
    bool parco = true;
    set<int> s;
    for (int i = 0; i < n; i++) {
      int val = a[i];
      set<int> gett = getFactorization(val);
      for (int tmp : gett) {
        if (s.find(tmp) != s.end()) {
          parco = false;
          goto out;
        }
        s.insert(tmp);
      }
    }
  out:
    if (parco) {
      cout << "pairwise coprime\\n";
    } else {
      if (allgcd != 1) {
        cout << "not coprime\\n";
      } else {
        cout << "setwise coprime\\n";
      }
    }
  }

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define endl '\\n'
#define fastIO                                                                 \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define int long long int
#define pb push_back
#define ff first
#define ss second
#define all(v) (v).begin(), (v).end()
#define mod (int)(998244353)
#define PI 3.14159265358979323846264338327950L
// ------------------ Debugging ------------------
#define trace(args...)                                                         \\
  {                                                                            \\
    string _s = #args;                                                         \\
    replace(_s.begin(), _s.end(), ',', ' ');                                   \\
    stringstream _ss(_s);                                                      \\
    istream_iterator<string> _it(_ss);                                         \\
    err(_it, args);                                                            \\
    cout << endl;                                                              \\
  }
void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cerr << "[ " << *it << " = " << a << " ] ";
  err(++it, args...);
}
template <typename Tk, typename Tv>
ostream &operator<<(ostream &os, const pair<Tk, Tv> &p) {
  os << "{" << p.first << ',' << p.second << "}";
  return os;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &p) {
  os << "[ ";
  for (T x : p)
    os << x << " ";
  os << "]" << endl;
  return os;
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &p) {
  os << "{ ";
  for (T x : p)
    os << x << " ";
  os << "}" << endl;
  return os;
}
template <typename Tk, typename Tv>
ostream &operator<<(ostream &os, const map<Tk, Tv> &p) {
  os << "{ ";
  for (pair<Tk, Tv> x : p)
    os << x << " ";
  os << "}" << endl;
  return os;
}
// -----------------------------------------------
int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
int lcm(int a, int b) { return (a * b) / gcd(a, b); }
const int MAXN = 1e4 + 5;
int spf[MAXN];

// Calculating SPF (Smallest Prime Factor) for every
// number till MAXN.
// Time Complexity : O(nloglogn)
void sieve() {
  spf[1] = 1;
  for (int 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 (int i = 4; i < MAXN; i += 2)
    spf[i] = 2;

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

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

// A O(log n) function returning primefactorization
// by dividing by smallest prime factor at every step
set<int> getFactorization(int x) {
  set<int> ret;
  while (x != 1) {
    ret.insert(spf[x]);
    x = x / spf[x];
  }
  return ret;
}
signed main() {

  fastIO

      int t = 1;
  // cin >> t;
  while (t--) {
    int n;
    cin >> n;
    sieve();
    vector<int> a(n);
    cin >> a[0];
    int allgcd = a[0];
    for (int i = 1; i < n; i++) {
      cin >> a[i];
      allgcd = gcd(allgcd, a[i]);
    }
    bool parco = true;
    set<int> s;
    for (int i = 0; i < n; i++) {
      int val = a[i];
      set<int> gett = getFactorization(val);
      for (int tmp : gett) {
        if (s.find(tmp) != s.end()) {
          parco = false;
          goto out;
        }
        s.insert(tmp);
      }
    }
  out:
    if (parco) {
      cout << "pairwise coprime\\n";
    } else {
      if (allgcd != 1) {
        cout << "not coprime\\n";
      } else {
        cout << "setwise coprime\\n";
      }
    }
  }

  return 0;
}