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

/*
AUTHOR: Naman
*/
#include <bits/stdc++.h>

using namespace std;
#define endl "\\n"
#define ll long long
#define ld long double
#define pll pair<long long, long long>
#define vll vector<long long>
#define all(x) x.begin(), x.end()
#define pb push_back
#define Fo(i, k, n) for (ll i = k; i <= n; i += 1)
#define deb(x) cout << #x << "=" << x << endl
#define in(a, n) Fo(i, 0, n - 1) cin >> a[i]
#define out(a, n)                                                              \\
  Fo(i, 0, n - 1) cout << a[i] << " ";                                         \\
  cout << endl
#define IOS                                                                    \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(NULL);
const ll N = 1e6 + 1;
const ll M = 1e9 + 7;

ll Gcd(ll a, ll b) {
  if (a == 0)
    return b;
  return Gcd(b % a, a);
}

int main() {
  IOS ll t = 1;
  // cin>>t;

  Fo(_, 1, t) {
    // cout<<"Case #"<<_<<": ";
    ll n;
    cin >> n;
    ll ar[n];
    in(ar, n);
    ll gcd = 0;
    Fo(i, 0, n - 1) gcd = Gcd(gcd, ar[i]);
    map<ll, ll> sm_fac;
    Fo(i, 2, N) sm_fac[i] = i;

    for (ll i = 2; i * i <= N; i++) {
      if (sm_fac[i] != i)
        continue;

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

    map<ll, ll> vis;

    Fo(i, 0, n - 1) {
      ll cur = ar[i];
      while (cur != 1) {
        ll fac = 1;
        fac = sm_fac[cur];
        // deb(fac);
        if (vis.find(fac) != vis.end()) {
          if (gcd == 1)
            cout << "setwise coprime";
          else
            cout << "not coprime";
          return 0;
        }
        vis[fac] = 1;
        while (fac && cur % fac == 0)
          cur /= fac;
      }
    }

    cout << "pairwise coprime";

    cout << endl;
  }
}

Error: TLE

Fixed submission:

/*
AUTHOR: Naman
*/
#include <bits/stdc++.h>

using namespace std;
#define endl "\\n"
#define ll long long
#define ld long double
#define pll pair<long long, long long>
#define vll vector<long long>
#define all(x) x.begin(), x.end()
#define pb push_back
#define Fo(i, k, n) for (ll i = k; i <= n; i += 1)
#define deb(x) cout << #x << "=" << x << endl
#define in(a, n) Fo(i, 0, n - 1) cin >> a[i]
#define out(a, n)                                                              \\
  Fo(i, 0, n - 1) cout << a[i] << " ";                                         \\
  cout << endl
#define IOS                                                                    \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(NULL);
const ll N = 1e6 + 1;
const ll M = 1e9 + 7;

ll Gcd(ll a, ll b) {
  if (a == 0)
    return b;
  return Gcd(b % a, a);
}

int main() {
  IOS ll t = 1;
  // cin>>t;

  Fo(_, 1, t) {
    // cout<<"Case #"<<_<<": ";
    ll n;
    cin >> n;
    ll ar[n];
    in(ar, n);
    ll gcd = 0;
    Fo(i, 0, n - 1) gcd = Gcd(gcd, ar[i]);
    map<ll, ll> sm_fac;

    for (ll i = 2; i * i <= N; i++) {
      if (sm_fac[i])
        continue;
      sm_fac[i] = i;
      for (ll j = i * i; j <= N; j += i) {
        if (!sm_fac[j])
          sm_fac[j] = i;
      }
    }

    map<ll, ll> vis;

    Fo(i, 0, n - 1) {
      ll cur = ar[i];
      while (cur != 1) {
        ll fac = 1;
        fac = sm_fac[cur];
        // deb(fac);
        if (vis.find(fac) != vis.end()) {
          if (gcd == 1)
            cout << "setwise coprime";
          else
            cout << "not coprime";
          return 0;
        }
        vis[fac] = 1;
        while (cur % fac == 0)
          cur /= fac;
      }
    }

    cout << "pairwise coprime";

    cout << endl;
  }
}