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

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
// #define int long long
#define endl '\\n'
#define pb push_back
#define fi first
#define se second
#define all(c) (c).begin(), (c).end()
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef vector<int> vi;

template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define TRACE
#ifndef ONLINE_JUDGE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
#else
#define trace(...)
#endif

const ll inf = 2e18;
const int mod = 1e9 + 7;
const int N = 1e6 + 10;

bool m[N];
vi v[N];

signed main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);

  int n;
  cin >> n;
  for (int i = 2; i < N; i++) {
    for (int j = i; j < N; j += i) {
      v[j].pb(i);
    }
  }
  int x = 1;
  int g = 0;
  for (int i = 0; i < n; i++) {
    int cur;
    cin >> cur;
    g = __gcd(g, cur);
    for (auto it : v[cur]) {
      if (m[it]) {
        x = 0;
      }
      m[it] = 1;
    }
  }
  if (x == 1) {
    cout << "pairwise coprime";
  } else if (x == 0 && g == 1) {
    cout << "setwise coprime";
  } else {
    cout << "not coprime";
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define int long long
#define endl '\\n'
#define pb push_back
#define fi first
#define se second
#define all(c) (c).begin(), (c).end()
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef vector<int> vi;

template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define TRACE
#ifndef ONLINE_JUDGE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
#else
#define trace(...)
#endif

const ll inf = 2e18;
const int mod = 1e9 + 7;
const int N = 1e6 + 10;

bool m[N];

signed main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);

  int n;
  cin >> n;
  int x = 1;
  int g = 0;
  for (int i = 0; i < n; i++) {
    int cur;
    cin >> cur;
    g = __gcd(g, cur);
    for (int j = 2; j * j <= cur; j++) {
      if (cur % j == 0) {
        if (m[j] || m[cur / j]) {
          x = 0;
        }
      }
    }
    if (cur != 1 && m[cur]) {
      x = 0;
    }
    for (int j = 2; j * j <= cur; j++) {
      if (cur % j == 0) {
        m[j] = 1;
        m[cur / j] = 1;
      }
    }
    if (cur != 1) {
      m[cur] = 1;
    }
  }
  if (x == 1) {
    cout << "pairwise coprime";
  } else if (x == 0 && g == 1) {
    cout << "setwise coprime";
  } else {
    cout << "not coprime";
  }

  return 0;
}