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

#include "bits/stdc++.h"
using namespace std;
#define ll long long
#define MAXN 1000001

// stores smallest prime factor for every number
ll spf[MAXN];

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

  for (ll i = 3; i * i < MAXN; i++) {
    // checking if i is prime
    if (spf[i] == i) {
      // marking SPF for all numbers divisible by i
      for (ll 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

// driver program for above function
int main() {
  // precalculating Smallest Prime Factor
  sieve();
  ll f[1000001];
  memset(f, 0, sizeof(f));
  ll n;
  cin >> n;
  ll m = n;
  ll g = 0;

  while (m--) {
    ll x;
    cin >> x;

    if (!g)
      g = x;
    else
      g = __gcd(x, g);

    if (x == 1)
      continue;

    map<ll, bool> check;
    while (x != 1) {
      // cout<<spf[x]<<' ';

      if (!check[spf[x]]) {
        f[spf[x]]++;
        check[spf[x]] = 1;
      }
      x = x / spf[x];
    }
    // cout<<'\\n';
  }

  bool f1 = 1, f2 = 1;

  for (ll i = 2; i < 1000001; i++) {
    //  cout<<f[i]<<' ';
    if (f[i] > 1)
      f1 = 0;

    if (f[i] >= n)
      f2 = 0;
  }
  // cout<<f[3]<<'\\n';
  // cout<<f1<<' '<<f2<<'\\n';
  // cout<<'\\n';
  if (f1)
    cout << "pairwise coprime";

  else if (g == 1)
    cout << "setwise coprime";

  else
    cout << "not coprime";
}

Error: WA

Fixed submission:

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

#define MAXN 100001

// stores smallest prime factor for every number
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

// driver program for above function
int main() {
  // precalculating Smallest Prime Factor
  sieve();
  int f[1000001];
  memset(f, 0, sizeof(f));
  int n;
  cin >> n;
  int m = n;

  while (m--) {
    int x;
    cin >> x;
    map<int, bool> check;
    while (x != 1) {
      // cout<<spf[x]<<' ';

      if (!check[spf[x]]) {
        f[spf[x]]++;
        check[spf[x]] = 1;
      }
      x = x / spf[x];
    }
    // cout<<'\\n';
  }

  bool f1 = 1, f2 = 1;

  for (int i = 0; i < 10; i++) {
    //  cout<<f[i]<<' ';
    if (f[i] > 1)
      f1 = 0;

    if (f[i] >= n)
      f2 = 0;
  }
  // cout<<f[3]<<'\\n';
  // cout<<f1<<' '<<f2<<'\\n';
  // cout<<'\\n';
  if (!f1 && f2)
    cout << "setwise coprime";

  else if (f1)
    cout << "pairwise coprime";

  else
    cout << "not coprime";
}