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

// Library
#include <algorithm>
#include <iostream>
#include <map>
#include <vector>

using namespace std;

// TYPEDEFS
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef pair<int, int> pi;

// CONSTANTS
#define SZA 0
#define SZB 0

// MACRO
#define MAX(X, Y) (((X) > (Y)) ? (X) : (Y))
#define MIN(X, Y) (((X) < (Y)) ? (X) : (Y))
#define ABS(X) (((X) > 0) ? (X) : -(X))
#define YN(X) ((X) ? "Yes" : "No")
#define SWAP(T, X, Y)                                                          \\
  {                                                                            \\
    T t;                                                                       \\
    t = X;                                                                     \\
    X = Y;                                                                     \\
    Y = t;                                                                     \\
  }

#define F first
#define S second
#define PB push_back
#define MP make_pair

// Global Tables

// ull gPreProcess[SZA][SZB];

// Functions

void Print2DVector(vector<vector<int>> &v) {
  ull i, j;
  for (i = 0; i < v.size(); i++) {
    for (j = 0; j < v[i].size(); j++) {
      cout << v[i][j];
      if (j == v[i].size() - 1) {
        cout << endl;
      } else {
        cout << " ";
      }
    }
  }
}

void PrintVector(vi &v) {
  ull i;
  for (i = 0; i < v.size(); i++) {
    cout << v[i];
    if (i == v.size() - 1) {
      cout << endl;
    } else {
      cout << " ";
    }
  }
}

void PrintArr(int *a, ull N) {
  ull i;
  for (i = 0; i < N; i++) {
    cout << a[i];
    if (i == N - 1) {
      cout << endl;
    } else {
      cout << " ";
    }
  }
}

void PreProcessTables() {
  ull i, j;
  for (i = 0; i < SZA; i++) {
    for (j = 0; j < SZB; j++) {
      break;
    }
    break;
  }
}

void TC(int t) {
  long long N;
  cin >> N;
  long long val, max_val = 0;
  int A[1000001]{0};
  for (long long i = 0; i < N; i++) {
    cin >> val;
    A[val]++;
    max_val = max(max_val, val);
  }
  long long mx = 1, cnt;
  bool sieve[1000001]{false};
  for (long long i = 2; i < max_val + 1; i++) {
    if (!sieve[i]) {
      cnt = 0;
      for (long long j = i; j < max_val + 1; j += i) {
        sieve[j] = true;
        if (A[j]) {
          cnt += A[j];
        }
      }
      if (cnt > 0) {
        mx = max(mx, cnt);
      }
    }
  }
  if (mx == 1) {
    cout << "pairwise coprime" << endl;
  } else if (mx == N) {
    cout << "not coprime" << endl;
  } else {
    cout << "setwise coprime" << endl;
  }
}

// Main

int main(void) {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  PreProcessTables();

  int T = 1, t;
  for (t = 1; t <= T; t++) {
    TC(t);
  }
  return 0;
}


Error: TLE

Fixed submission:

// Library
#include <algorithm>
#include <iostream>
#include <map>
#include <vector>

using namespace std;

// TYPEDEFS
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef pair<int, int> pi;

// CONSTANTS
#define SZA 0
#define SZB 0

// MACRO
#define MAX(X, Y) (((X) > (Y)) ? (X) : (Y))
#define MIN(X, Y) (((X) < (Y)) ? (X) : (Y))
#define ABS(X) (((X) > 0) ? (X) : -(X))
#define YN(X) ((X) ? "Yes" : "No")
#define SWAP(T, X, Y)                                                          \\
  {                                                                            \\
    T t;                                                                       \\
    t = X;                                                                     \\
    X = Y;                                                                     \\
    Y = t;                                                                     \\
  }

#define F first
#define S second
#define PB push_back
#define MP make_pair

// Global Tables

// ull gPreProcess[SZA][SZB];

// Functions

void Print2DVector(vector<vector<int>> &v) {
  ull i, j;
  for (i = 0; i < v.size(); i++) {
    for (j = 0; j < v[i].size(); j++) {
      cout << v[i][j];
      if (j == v[i].size() - 1) {
        cout << endl;
      } else {
        cout << " ";
      }
    }
  }
}

void PrintVector(vi &v) {
  ull i;
  for (i = 0; i < v.size(); i++) {
    cout << v[i];
    if (i == v.size() - 1) {
      cout << endl;
    } else {
      cout << " ";
    }
  }
}

void PrintArr(int *a, ull N) {
  ull i;
  for (i = 0; i < N; i++) {
    cout << a[i];
    if (i == N - 1) {
      cout << endl;
    } else {
      cout << " ";
    }
  }
}

void PreProcessTables() {
  ull i, j;
  for (i = 0; i < SZA; i++) {
    for (j = 0; j < SZB; j++) {
      break;
    }
    break;
  }
}

int find(int *st, int a) {
  if (a == st[a]) {
    return a;
  } else {
    st[a] = find(st, st[a]);
    return st[a];
  }
}

void unite(int *st, int *sz, int a, int b) {
  a = find(st, a);
  b = find(st, b);
  if (a == b) {
    return;
  }
  if (sz[a] < sz[b]) {
    int t = a;
    a = b;
    b = t;
  }
  st[b] = a;
  sz[a] += b;
  sz[b] = 0;
}

void TC(int t) {
  int N;
  cin >> N;
  int A[N];
  // int st[N], sz[N];
  vector<int> primes;
  map<int, vector<int>> m;
  {
    bool sieve[1000001] = {
        false,
    };
    for (int i = 2; i < 1000001; i += (i > 2) ? 2 : 1) {
      if (!sieve[i]) {
        primes.push_back(i);
        for (int j = 2 * i; j < 1000001; j += i) {
          sieve[j] = true;
        }
      }
    }
  }
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    // st[i] = i;
    // sz[i] = 1;
  }
  int mn = 1, mx = 1;
  for (int i = 0; i < N; i++) {
    for (int j : primes) {
      if (j > A[i] || A[i] == 1) {
        break;
      }
      if (A[i] % j == 0) {
        if (m.find(j) == m.end()) {
          m[j] = {i};
        } else {
          mx = 0;
          m[j].push_back(i);
          if (m[j].size() == N) {
            mn = 0;
          }
          // unite(st, sz, m[j][0], i);
        }
        while (A[i] % j == 0) {
          A[i] /= j;
        }
      }
    }
  }
  // for(int i = 0; i < N; i++)
  //{
  //     mx = max(mx, sz[i]);
  //     if(mx > 1)
  //     {
  //         break;
  //     }
  // }
  if (mx == 1) {
    cout << "pairwise coprime" << endl;
  } else if (mn == 1) {
    cout << "setwise coprime" << endl;
  } else {
    cout << "not coprime" << endl;
  }
}

// Main

int main(void) {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  PreProcessTables();

  int T = 1, t;
  for (t = 1; t <= T; t++) {
    TC(t);
  }
  return 0;
}