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

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ll long long
#define ld long double
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
using P = pair<long long, long long>;
#define rep(i, n) for (long long i = 0; i < (long long)n; i++)
#define req(i, n) for (long long i = n - 1; i >= 0; i--)
#define range(i, a, b) for (long long i = a; i < b; i++)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((long long)(x).size())
#define COUT(x) cout << x << endl
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define onBoard(y, x) (y >= 0 && y < h && x >= 0 && x < w)
#define pri_que priority_queue
#define vint vector<int>
#define vvint vector<vector<int>>
#define vi vector<int>
#define vvi vector<vector<int>>
#define vs vector<string>
#define vvc vector<vector<char>>
#define vc vector<char>
#define vp vector<pair<int, int>>
#define vb vector<bool>
#define vvb vector<vector<bool>>
#define show(x) cout << #x << "=" << x << endl;
#define SUM(x) accumulate(x.begin(), x.end(), 0)
#define MAX(x) *max_element(x.begin(), x.end())
#define MIN(x) *min_element(x.begin(), x.end())
#define couty cout << "Yes" << endl
#define coutn cout << "No" << endl
#define coutY cout << "YES" << endl
#define coutN cout << "NO" << endl
#define yn(x) cout << (x ? "Yes" : "No") << endl
#define YN(x) cout << (x ? "YES" : "NO") << endl
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
const long long dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const long long dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
const long long INF = 1e15;
const long long MOD = 1e9 + 7;

const int MAX = 1e6 + 1;

signed main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);

  int n;
  cin >> n;
  vi a(n);
  rep(i, n) cin >> a[i];

  vi dp(MAX, -1);
  dp[1] = 1;
  for (int i = 2; i < MAX; i++) {
    if (dp[i] != -1)
      continue;
    for (int j = i; j <= MAX; j += i) {
      dp[j] = i;
    }
  }

  vi sum(MAX, 0);
  bool pc = true;
  rep(i, n) {
    int t = a[i];
    while (t > 1) {
      int div = dp[t];
      while (t % div == 0)
        t /= div;
      sum[div]++;
      if (sum[div] > 1) {
        pc = false;
        break;
      }
    }
    if (pc == false)
      break;
  }

  int sc = a[0];
  for (int i = 1; i < n; i++)
    sc = gcd(sc, a[i]);

  if (pc)
    cout << "pairwise coprime" << endl;
  else if (sc == 1)
    cout << "setwise coprime" << endl;
  else
    cout << "not coprime" << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ll long long
#define ld long double
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
using P = pair<long long, long long>;
#define rep(i, n) for (long long i = 0; i < (long long)n; i++)
#define req(i, n) for (long long i = n - 1; i >= 0; i--)
#define range(i, a, b) for (long long i = a; i < b; i++)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((long long)(x).size())
#define COUT(x) cout << x << endl
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define onBoard(y, x) (y >= 0 && y < h && x >= 0 && x < w)
#define pri_que priority_queue
#define vint vector<int>
#define vvint vector<vector<int>>
#define vi vector<int>
#define vvi vector<vector<int>>
#define vs vector<string>
#define vvc vector<vector<char>>
#define vc vector<char>
#define vp vector<pair<int, int>>
#define vb vector<bool>
#define vvb vector<vector<bool>>
#define show(x) cout << #x << "=" << x << endl;
#define SUM(x) accumulate(x.begin(), x.end(), 0)
#define MAX(x) *max_element(x.begin(), x.end())
#define MIN(x) *min_element(x.begin(), x.end())
#define couty cout << "Yes" << endl
#define coutn cout << "No" << endl
#define coutY cout << "YES" << endl
#define coutN cout << "NO" << endl
#define yn(x) cout << (x ? "Yes" : "No") << endl
#define YN(x) cout << (x ? "YES" : "NO") << endl
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
const long long dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const long long dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
const long long INF = 1e15;
const long long MOD = 1e9 + 7;

const int MAX = 1e6 + 1;

signed main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);

  int n;
  cin >> n;
  vi a(n);
  rep(i, n) cin >> a[i];

  vi dp(MAX, -1);
  dp[1] = 1;
  for (int i = 2; i * i <= MAX; i++) {
    if (dp[i] != -1)
      continue;
    for (int j = i; j <= MAX; j += i) {
      dp[j] = i;
    }
  }

  vi sum(MAX, 0);
  bool pc = true;
  rep(i, n) {
    int t = a[i];
    while (t > 1) {
      int div = dp[t];
      while (t % div == 0)
        t /= div;
      sum[div]++;
      if (sum[div] > 1) {
        pc = false;
        break;
      }
    }
    if (pc == false)
      break;
  }

  int sc = a[0];
  for (int i = 1; i < n; i++)
    sc = gcd(sc, a[i]);

  if (pc)
    cout << "pairwise coprime" << endl;
  else if (sc == 1)
    cout << "setwise coprime" << endl;
  else
    cout << "not coprime" << endl;
}