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

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

#define ll long long int
#define f(i, x, n) for (int i = x; i < n; i++)
#define FASTIO                                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0);                                                                 \\
  ios_base::sync_with_stdio(false);
#define sz(a) int((a).size())
#define mod 1000000007
#define inf LLONG_MAX
#define error(args...)                                                         \\
  {                                                                            \\
    string _s = #args;                                                         \\
    replace(_s.begin(), _s.end(), ',', ' ');                                   \\
    stringstream _ss(_s);                                                      \\
    istream_iterator<string> _it(_ss);                                         \\
    err(_it, args);                                                            \\
  }
template <typename T> inline T gcd(T a, T b) {
  while (b != 0)
    swap(b, a %= b);
  return a;
}

template <typename T> inline void seethis(vector<T> vect) {
  for (T x : vect)
    cout << x << " ";
  cout << "\\n";
}

void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cerr << *it << " = " << a << endl;
  err(++it, args...);
}
// LCM
int lcm(int a, int b) { return a * (b / gcd(a, b)); }

// For sort by second
bool cmp(const pair<int, int> &a) { return a.first < a.second; }

// Tree
/* const int MAXN = 1e5 + 5;
vector<ll> adj[MAXN];*/

// ModPower
ll modpower(ll a, ll b, ll c) {
  ll res = 1;
  while (b) {
    if (b & 1LL)
      res = (res * a) % c;
    a = (a * a) % c;
    b >>= 1;
  }
  return res;
}

const long long MAX_SIZE = 1e6 + 1;

// isPrime[] : isPrime[i] is true if number is prime
// prime[] : stores all prime number less than N
// SPF[] that store smallest prime factor of number
// [for Exp : smallest prime factor of '8' and '16'
// is '2' so we put SPF[8] = 2 , SPF[16] = 2 ]
vector<long long> isprime(MAX_SIZE, true);
vector<long long> prime;
vector<long long> SPF(MAX_SIZE);

// function generate all prime number less then N in O(n)
void manipulated_seive(ll N) {

  isprime[0] = isprime[1] = false;
  SPF[1] = 1;

  for (long long int i = 2; i < N; i++) {

    if (isprime[i]) {
      // put i into prime[] vector
      prime.push_back(i);

      // A prime number is its own smallest
      // prime factor
      SPF[i] = i;
    }

    for (long long int j = 0;
         j < (int)prime.size() && i * prime[j] < N && prime[j] <= SPF[i]; j++) {
      isprime[i * prime[j]] = false;

      // put smallest prime factor of i*prime[j]
      SPF[i * prime[j]] = prime[j];
    }
  }
}

ll maxn = 1e6;
ll n;
vector<ll> vect(maxn);

int main() {
  FASTIO;
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  manipulated_seive(1000001);

  // cout<<SPF[1000];
  cin >> n;

  for (ll i = 0; i < n; i++)
    cin >> vect[i];

  set<ll> se;

  bool pw_co = true;

  ll x = vect[0];

  for (ll i = 0; i < n; i++) {

    ll temp = vect[i];
    unordered_set<ll> se2;

    while (temp > 1) {

      ll factor = SPF[temp];
      // cout<<factor<<" ";
      if (se.find(factor) == se.end()) {
        se2.insert(factor);
        temp /= factor;
      } else {
        pw_co = false;
        break;
      }
    }

    if (!pw_co)
      break;
    for (ll x : se2)
      se.insert(x);
  }

  if (pw_co) {
    cout << "pairwise coprime\\n";
  } else {
    for (int i = 1; i < n; i++)
      x = gcd(x, vect[i]);

    if (x == 1) {
      cout << "setwise coprime\\n";
    } else
      cout << "not coprime\\n";
  }

  return 0;
}

Error: WA

Fixed submission:

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

#define ll long long int
#define f(i, x, n) for (int i = x; i < n; i++)
#define FASTIO                                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0);                                                                 \\
  ios_base::sync_with_stdio(false);
#define sz(a) int((a).size())
#define mod 1000000007
#define inf LLONG_MAX
#define error(args...)                                                         \\
  {                                                                            \\
    string _s = #args;                                                         \\
    replace(_s.begin(), _s.end(), ',', ' ');                                   \\
    stringstream _ss(_s);                                                      \\
    istream_iterator<string> _it(_ss);                                         \\
    err(_it, args);                                                            \\
  }
template <typename T> inline T gcd(T a, T b) {
  while (b != 0)
    swap(b, a %= b);
  return a;
}

template <typename T> inline void seethis(vector<T> vect) {
  for (T x : vect)
    cout << x << " ";
  cout << "\\n";
}

void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cerr << *it << " = " << a << endl;
  err(++it, args...);
}
// LCM
int lcm(int a, int b) { return a * (b / gcd(a, b)); }

// For sort by second
bool cmp(const pair<int, int> &a) { return a.first < a.second; }

// Tree
/* const int MAXN = 1e5 + 5;
vector<ll> adj[MAXN];*/

// ModPower
ll modpower(ll a, ll b, ll c) {
  ll res = 1;
  while (b) {
    if (b & 1LL)
      res = (res * a) % c;
    a = (a * a) % c;
    b >>= 1;
  }
  return res;
}

const long long MAX_SIZE = 1e6 + 1;

// isPrime[] : isPrime[i] is true if number is prime
// prime[] : stores all prime number less than N
// SPF[] that store smallest prime factor of number
// [for Exp : smallest prime factor of '8' and '16'
// is '2' so we put SPF[8] = 2 , SPF[16] = 2 ]
vector<long long> isprime(MAX_SIZE, true);
vector<long long> prime;
vector<long long> SPF(MAX_SIZE);

// function generate all prime number less then N in O(n)
void manipulated_seive(ll N) {

  isprime[0] = isprime[1] = false;
  SPF[1] = 1;

  for (long long int i = 2; i < N; i++) {

    if (isprime[i]) {
      // put i into prime[] vector
      prime.push_back(i);

      // A prime number is its own smallest
      // prime factor
      SPF[i] = i;
    }

    for (long long int j = 0;
         j < (int)prime.size() && i * prime[j] < N && prime[j] <= SPF[i]; j++) {
      isprime[i * prime[j]] = false;

      // put smallest prime factor of i*prime[j]
      SPF[i * prime[j]] = prime[j];
    }
  }
}

ll maxn = 1e6;
ll n;
vector<ll> vect(maxn);

int main() {
  FASTIO;
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  manipulated_seive(1000000);

  // cout<<SPF[1000];
  cin >> n;

  for (ll i = 0; i < n; i++)
    cin >> vect[i];

  set<ll> se;

  bool pw_co = true;

  ll x = vect[0];

  for (ll i = 0; i < n; i++) {

    ll temp = vect[i];
    x = gcd(x, temp);

    while (temp > 1) {

      ll factor = SPF[temp];

      if (se.find(factor) == se.end()) {
        se.insert(factor);
        temp /= factor;
      } else {
        pw_co = false;
        break;
      }
    }
  }

  if (pw_co) {
    cout << "pairwise coprime\\n";
  } else if (x == 1) {
    cout << "setwise coprime\\n";
  } else {
    cout << "not coprime\\n";
  }

  return 0;
}