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

#include <bits/stdc++.h>
using namespace std;

#define ALL(a) (a.begin(), a.end())
#define ll int
#define ff first
#define ss second
#define pb push_back
#define ar array
#define mp make_pair
#define f(i, a, b) for (ll i = a; i <= b; i++)
#define inf INT_MAX
#define pll pair<ll, ll>
#define mat vector<vector<ll>>
/*
#define sim template < class c
#define ris return * this
#define dor > debug & operator <<
#define eni(x) sim > typename \\
  enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
sim > struct rge { c b, e; };
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c* x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifndef ONLINE_JUDGE
~debug() { cerr << endl; }
eni(!=) cerr << boolalpha << i; ris; }
eni(==) ris << range(begin(i), end(i)); }
sim, class b dor(pair < b, c > d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (auto it = d.b; it != d.e; ++it)
        *this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
#else
sim dor(const c&) { ris; }
#endif
};
#define deb(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
 //debug & operator << (debug & dd, P p) { dd << "(" << p.x << ", " << p.y <<
")"; return dd; }

 */

// const ll INF = 1e18;
void init() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
}

bool checkp2(ll n) { return n && !(n & (n - 1)); }

ll numsetbit(ll n) {
  ll count = 0;
  for (; n; count++) {
    n = n & (n - 1);
  }
  return count;
}

ll power(ll x, ll n, ll mod) {
  x = x % mod;
  ll po = 1;
  while (n) {
    if (n & 1) {
      po = (po * x) % mod;
    }
    n = n >> 1;
    x = ((x % mod) * (x % mod)) % mod;
  }
  return po;
}

bool isPrime(ll x) {
  if (x < 2)
    return 0;
  for (ll y = 2; y * y <= x; y++) {
    if (x % y == 0) {
      return 0;
    }
  }
  return true;
}

const ll mxn = (int)1e6;
ll spf[mxn + 10];

void solve() {
  // cout<<"stating\\n";
  spf[1] = 1;
  // cout<<"hmm\\n";
  for (ll i = 2; i <= mxn; i++) {
    spf[i] = i;
  }
  // cout<<"hello\\n";
  for (ll i = 4; i <= mxn; i += 2) {
    spf[i] = 2;
  }
  // cout<<"hii\\n";
  for (ll i = 3; i * i <= mxn; i++) {

    if (spf[i] == i) {
      for (ll j = i * i; j <= mxn; j += i) {
        if (spf[j] == j) {
          spf[j] = i;
        }
      }
    }
  }

  ll n;
  cin >> n;
  // cout<<n<<endl;
  vector<ll> a(n);
  for (ll i = 0; i < n; i++) {
    cin >> a[i];
  }

  ll hash[mxn] = {0};
  bool flag = 0;
  for (ll i = 0; i < n; i++) {

    ll x = a[i];
    ll temp = spf[x];

    while (x != 1) {
      temp = spf[x];
      if (x % temp == 0) {
        if (hash[spf[x]] == 1) {
          flag = 1;
        }
        hash[spf[x]]++;
      }
      while (x % temp == 0) {
        x = x / temp;
      }
      if (flag)
        break;
    }
    if (flag)
      break;
  }
  // debug()<< deb(flag);
  if (!flag) {
    cout << "pairwise coprime\\n" << endl;
    return;
  }
  ll answer = a[0];
  for (ll i = 1; i < n; i++) {
    answer = __gcd(answer, a[i]);
  }
  if (answer == 1) {
    cout << "setwise coprime\\n";
    return;
  }

  cout << "not coprime\\n" << endl;
}

int main() {

  init();
  ll t = 1;
  // cin>>t;
  for (ll i = 1; i <= t; i++) {
    // cout<<"Case #"<<i<<": ";
    // cout<<"solve\\n";
    solve();
  }
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define ALL(a) (a.begin(), a.end())
#define ll int
#define ff first
#define ss second
#define pb push_back
#define ar array
#define mp make_pair
#define f(i, a, b) for (ll i = a; i <= b; i++)
#define inf INT_MAX
#define pll pair<ll, ll>
#define mat vector<vector<ll>>
/*
#define sim template < class c
#define ris return * this
#define dor > debug & operator <<
#define eni(x) sim > typename \\
  enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
sim > struct rge { c b, e; };
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c* x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifndef ONLINE_JUDGE
~debug() { cerr << endl; }
eni(!=) cerr << boolalpha << i; ris; }
eni(==) ris << range(begin(i), end(i)); }
sim, class b dor(pair < b, c > d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (auto it = d.b; it != d.e; ++it)
        *this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
#else
sim dor(const c&) { ris; }
#endif
};
#define deb(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
 //debug & operator << (debug & dd, P p) { dd << "(" << p.x << ", " << p.y <<
")"; return dd; }

 */

// const ll INF = 1e18;
void init() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
}

bool checkp2(ll n) { return n && !(n & (n - 1)); }

ll numsetbit(ll n) {
  ll count = 0;
  for (; n; count++) {
    n = n & (n - 1);
  }
  return count;
}

ll power(ll x, ll n, ll mod) {
  x = x % mod;
  ll po = 1;
  while (n) {
    if (n & 1) {
      po = (po * x) % mod;
    }
    n = n >> 1;
    x = ((x % mod) * (x % mod)) % mod;
  }
  return po;
}

bool isPrime(ll x) {
  if (x < 2)
    return 0;
  for (ll y = 2; y * y <= x; y++) {
    if (x % y == 0) {
      return 0;
    }
  }
  return true;
}

const ll mod = (int)1e9 + 7;
const ll mxn = 4e5;
ll spf[mxn + 7];

void solve() {

  spf[1] = 1;
  // cout<<"hmm\\n";
  for (ll i = 2; i <= mxn; i++) {
    spf[i] = i;
  }
  // cout<<"hello\\n";
  for (ll i = 4; i <= mxn; i += 2) {
    spf[i] = 2;
  }
  // cout<<"hii\\n";
  for (ll i = 3; i * i <= mxn; i++) {

    if (spf[i] == i) {
      for (ll j = i * i; j <= mxn; j += i) {
        if (spf[j] == j) {
          spf[j] = i;
        }
      }
    }
  }

  ll n;
  cin >> n;
  // cout<<n<<endl;
  vector<ll> a(n);
  for (ll i = 0; i < n; i++) {
    cin >> a[i];
  }

  ll hash[mxn] = {0};
  bool flag = 0;
  for (ll i = 0; i < n; i++) {

    ll x = a[i];
    ll temp = spf[x];

    while (x != 1) {
      temp = spf[x];
      if (x % temp == 0) {
        if (hash[spf[x]] == 1) {
          flag = 1;
        }
        hash[spf[x]]++;
      }
      while (x % temp == 0) {
        x = x / temp;
      }
      if (flag)
        break;
    }
    if (flag)
      break;
  }
  // debug()<< deb(flag);
  if (!flag) {
    cout << "pairwise coprime\\n" << endl;
    return;
  }
  ll answer = a[0];
  for (ll i = 1; i < n; i++) {
    answer = __gcd(answer, a[i]);
  }
  if (answer == 1) {
    cout << "setwise coprime\\n";
    return;
  }

  cout << "not coprime\\n" << endl;
}

int main() {

  init();
  ll t = 1;
  // cin>>t;
  for (ll i = 1; i <= t; i++) {
    // cout<<"Case #"<<i<<": ";
    // cout<<"solve\\n";
    solve();
  }
}