Problem p02574 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef map<ll, ll>::iterator itll;
typedef long double ld;
typedef map<ll, ll> mapll;

#define con continue
#define pb push_back
#define fi first
#define se second
#define fr front()
#define INF 1000000000000000000
#define all(vl) vl.begin(), vl.end()
#define m_p make_pair
#define sz(a) sizeof(a)
#define forn(mp, it) for (it = mp.begin(); it != mp.end(); it++)
#define FOR(i, a, n) for (ll i = a; i < n; i++)
#define FORE(i, a, n) FOR(i, a, n + 1)
#define Endl endl
#define eNdl endl
#define enDl endl
#define endL endl

bool isp[1001000];
ll gcd(ll a, ll b) { return (b == 0 ? a : gcd(b, a % b)); }

bool setwise_coprime(vector<ll> vl) {
  ll Gcd = vl[0];
  FOR(i, 1, vl.size()) { Gcd = gcd(Gcd, vl[i]); }
  return Gcd == 1;
}

signed main() {
  isp[0] = isp[1] = 1;
  vector<ll> prime;
  for (ll i = 2; i <= 1000000; i++) {
    if (!isp[i])
      prime.push_back(i);
    for (ll j = i * i; j <= 1000000; j += i) {
      isp[j] = 1;
    }
  }
  ll n;
  cin >> n;
  map<ll, ll> mp;
  vector<ll> vl(n);
  ll other = 1000001;
  FOR(i, 0, n) {
    ll a;
    cin >> a;
    vl[i] = a;
    FOR(j, 0, prime.size()) {
      if (prime[j] * prime[j] > a) {
        break;
      }
      P cnt;
      cnt.fi = 0;
      cnt.se = 0;
      while (a % prime[j] == 0) {
        cnt.fi = prime[j];
        cnt.se++;
        a /= prime[j];
      }
      if (cnt.fi != 0) {
        mp[cnt.fi]++;
      }
    }
    if (a > 1) {
      mp[a]++;
    }
  }
  map<ll, ll>::iterator it, it1;
  bool isok = 1;
  forn(mp, it) {
    if (it->se > 1)
      isok = false;
  }
  if (isok) {
    cout << "pairwise coprime" << endl;
    return 0;
  }
  if (setwise_coprime(vl)) {
    cout << "setwise coprime" << endl;
    return 0;
  } else
    cout << "not coprime" << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef map<ll, ll>::iterator itll;
typedef long double ld;
typedef map<ll, ll> mapll;

#define con continue
#define pb push_back
#define fi first
#define se second
#define fr front()
#define INF 1000000000000000000
#define all(vl) vl.begin(), vl.end()
#define m_p make_pair
#define sz(a) sizeof(a)
#define forn(mp, it) for (it = mp.begin(); it != mp.end(); it++)
#define FOR(i, a, n) for (ll i = a; i < n; i++)
#define FORE(i, a, n) FOR(i, a, n + 1)
#define Endl endl
#define eNdl endl
#define enDl endl
#define endL endl

bool isp[1001000];
ll gcd(ll a, ll b) { return (b == 0 ? a : gcd(b, a % b)); }

bool setwise_coprime(vector<ll> vl) {
  ll Gcd = vl[0];
  FOR(i, 1, vl.size()) { Gcd = gcd(Gcd, vl[i]); }
  return Gcd == 1;
}

signed main() {
  isp[0] = isp[1] = 1;
  vector<ll> prime;
  for (ll i = 2; i <= 1000000; i++) {
    if (!isp[i])
      prime.push_back(i);
    for (ll j = i * i; j <= 1000000; j += i) {
      isp[j] = 1;
    }
  }
  ll n;
  cin >> n;
  map<ll, ll> mp;
  vector<ll> vl(n);
  ll other = 1000001;
  FOR(i, 0, n) {
    ll a;
    cin >> a;
    vl[i] = a;
    FOR(j, 0, prime.size()) {
      if (a == 1) {
        break;
      }
      P cnt;
      cnt.fi = 0;
      cnt.se = 0;
      while (a % prime[j] == 0) {
        cnt.fi = prime[j];
        cnt.se++;
        a /= prime[j];
      }
      if (cnt.fi != 0) {
        mp[cnt.fi]++;
      }
    }
  }
  map<ll, ll>::iterator it, it1;
  bool isok = 1;
  forn(mp, it) {
    if (it->se > 1)
      isok = false;
  }
  if (isok) {
    cout << "pairwise coprime" << endl;
    return 0;
  }
  if (setwise_coprime(vl)) {
    cout << "setwise coprime" << endl;
    return 0;
  } else
    cout << "not coprime" << endl;
  return 0;
}