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

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

using i64 = int64_t;
using P = pair<i64, i64>;
i64 mod = 1000000007;
// i64 mod = 998244353;
int int_max = 2147483647;
i64 INF = 1e16;
int max_n = 1e5;

template <typename T> T gcd(T a, T b) {
  if (a % b == 0)
    return b;
  return gcd(b, a % b);
}

template <typename T> void prime(T x, map<T, T> &cnt) {
  for (T i = 2; i * i <= x; ++i) {
    int c = 0;
    while (x % i == 0) {
      x /= i;
      c++;
    }
    if (c)
      cnt[i] += c;
  }
  if (x > 1)
    cnt[x] += 1;
}

int main(int argc, char **argv) {
  i64 n;
  cin >> n;
  vector<i64> a(n, 0), pr(1000000 + 1, 0);
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }

  bool unique{true};
  i64 max_pr{0};
  for (int i = 0; i < n; ++i) {
    i64 x = a[i];
    for (i64 j = 2; j * j <= x; ++j) {
      bool first{true};
      while (x % j == 0) {
        x /= j;
        if (first) {
          if (pr[j] != 0)
            unique = false;
          ++pr[j];
          max_pr = max(max_pr, pr[j]);
          first = false;
        }
      }
    }
    if (x > 1) {
      if (pr[x] != 0)
        unique = false;
      ++pr[x];
      max_pr = max(max_pr, pr[x]);
    }
  }
  if (max_pr == n) {
    cout << "not coprime" << endl;
    return 0;
  }
  if (unique) {
    cout << "pairwise coprime" << endl;
  } else {
    cout << "setwise coprime" << endl;
  }
  /*
  unique = false;
  pr = vector<i64>(1000000+1,0);
  for (int i=0; i<n; ++i) {
      map<i64, i64>& cnt = prime_cnt[i];
      bool is_prime{true};
      for (auto kv : cnt) {
          ++pr[kv.first];
          if (i==n-1 && pr[kv.first] == n) {
              cout << "not coprime" << endl;
              return 0;
          }
      }
  }
  cout << "setwise coprime" << endl;
  */

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

using i64 = int64_t;
using P = pair<i64, i64>;
i64 mod = 1000000007;
// i64 mod = 998244353;
int int_max = 2147483647;
i64 INF = 1e16;
int max_n = 1e5;

template <typename T> T gcd(T a, T b) {
  if (a % b == 0)
    return b;
  return gcd(b, a % b);
}

template <typename T> void prime(T x, map<T, T> &cnt) {
  for (T i = 2; i * i <= x; ++i) {
    int c = 0;
    while (x % i == 0) {
      x /= i;
      c++;
    }
    if (c)
      cnt[i] += c;
  }
  if (x > 1)
    cnt[x] += 1;
}

int main(int argc, char **argv) {
  i64 n;
  cin >> n;
  vector<i64> a(n, 0), pr(1000000 + 1, 0);
  vector<map<i64, i64>> prime_cnt(n, map<i64, i64>());
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }

  bool unique{true};
  i64 max_pr{0};
  for (int i = 0; i < n; ++i) {
    map<i64, i64> &cnt = prime_cnt[i];
    prime(a[i], cnt);
    i64 x = a[i];
    for (i64 j = 2; j * j <= x; ++j) {
      bool first{true};
      while (x % j == 0) {
        x /= j;
        if (first) {
          if (pr[j] != 0)
            unique = false;
          ++pr[j];
          max_pr = max(max_pr, pr[j]);
          first = false;
        }
      }
    }
    if (x > 1) {
      if (pr[x] != 0)
        unique = false;
      ++pr[x];
      max_pr = max(max_pr, pr[x]);
    }
  }
  if (max_pr == n) {
    cout << "not coprime" << endl;
    return 0;
  }
  if (unique) {
    cout << "pairwise coprime" << endl;
  } else {
    cout << "setwise coprime" << endl;
  }
  /*
  unique = false;
  pr = vector<i64>(1000000+1,0);
  for (int i=0; i<n; ++i) {
      map<i64, i64>& cnt = prime_cnt[i];
      bool is_prime{true};
      for (auto kv : cnt) {
          ++pr[kv.first];
          if (i==n-1 && pr[kv.first] == n) {
              cout << "not coprime" << endl;
              return 0;
          }
      }
  }
  cout << "setwise coprime" << endl;
  */

  return 0;
}