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

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ext/pb_ds/assoc_container.hpp>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;
using namespace __gnu_pbds;

#define ll long long
#define ull unsigned long long
#define pb push_back
#define eb emplace_back
#define ff first
#define ss second
#define rep(i, a, b) for (int i = (a); i < (b); ++i)
#define repp(i, a, b) for (int i = (a); i >= b; --i)
#define each(x, a) for (auto &x : a)
#define len(x) (int)x.size()
#define all(x) (x).begin(), (x).end()
#define mem(x, a) memset(x, a, sizeof(x))
#define vi vector<int>
#define vl vector<LL>
#define pii pair<int, int>
#define Pll pair<LL, LL>
#define io                                                                     \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);

const ll MOD = 1e9 + 7;
const ll INF = 1e18;

typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    pbds;

string to_string(string s) { return '"' + s + '"'; }
string to_string(const char *s) { return to_string((string)s); }
string to_string(bool b) { return (b ? "true" : "false"); }
template <typename A, typename B> string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A> string to_string(A v) {
  bool first = true;
  string res = "{";
  for (const auto &x : v) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(x);
  }
  res += "}";
  return res;
}
void debug_out() { cerr << '\\n'; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}
#ifndef ONLINE_JUDGE
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(x...) ;
#endif

const int N = 1000010;
vi div_cnt(N);

void solve() {
  int n;
  cin >> n;
  vi arr(n);
  rep(i, 0, n) cin >> arr[i];
  bool ok = true;
  each(x, arr) {
    for (int i = 1; i * i < x; ++i) {
      if (x % i == 0) {
        div_cnt[i]++;
        if (i * i != x) {
          div_cnt[x / i]++;
        }
        if (i != 1 && div_cnt[i] > 1) {
          ok = false;
          break;
        } else if ((x / i) != 1 && div_cnt[x / i] > 1) {
          ok = false;
          break;
        }
      }
      if (!ok)
        break;
    }
  }
  if (ok) {
    cout << "pairwise coprime"
         << "\\n";
    return;
  }
  int g = 0;
  rep(i, 0, n) g = __gcd(arr[i], g);
  if (g == 1) {
    cout << "setwise coprime"
         << "\\n";
    return;
  }
  cout << "not coprime"
       << "\\n";
}
int main() {
  io;
  // freopen("in.txt" ,"r" , stdin) ;
  solve();
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ext/pb_ds/assoc_container.hpp>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;
using namespace __gnu_pbds;

#define ll long long
#define ull unsigned long long
#define pb push_back
#define eb emplace_back
#define ff first
#define ss second
#define rep(i, a, b) for (int i = (a); i < (b); ++i)
#define repp(i, a, b) for (int i = (a); i >= b; --i)
#define each(x, a) for (auto &x : a)
#define len(x) (int)x.size()
#define all(x) (x).begin(), (x).end()
#define mem(x, a) memset(x, a, sizeof(x))
#define vi vector<int>
#define vl vector<LL>
#define pii pair<int, int>
#define Pll pair<LL, LL>
#define io                                                                     \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);

const ll MOD = 1e9 + 7;
const ll INF = 1e18;

typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    pbds;

string to_string(string s) { return '"' + s + '"'; }
string to_string(const char *s) { return to_string((string)s); }
string to_string(bool b) { return (b ? "true" : "false"); }
template <typename A, typename B> string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A> string to_string(A v) {
  bool first = true;
  string res = "{";
  for (const auto &x : v) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(x);
  }
  res += "}";
  return res;
}
void debug_out() { cerr << '\\n'; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}
#ifndef ONLINE_JUDGE
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(x...) ;
#endif

const int N = 1000010;
vi div_cnt(N);

void solve() {
  int n;
  cin >> n;
  vi arr(n);
  rep(i, 0, n) cin >> arr[i];
  each(x, arr) {
    for (int i = 1; i * i < x; ++i) {
      if (x % i == 0) {
        div_cnt[i]++;
        if (i * i != x) {
          div_cnt[x / i]++;
        }
      }
    }
  }
  bool ok = true;
  rep(i, 2, N) {
    if (div_cnt[i] > 1) {
      ok = false;
      break;
    }
  }
  if (ok) {
    cout << "pairwise coprime"
         << "\\n";
    return;
  }
  int g = 0;
  rep(i, 0, n) g = __gcd(arr[i], g);
  if (g == 1) {
    cout << "setwise coprime"
         << "\\n";
    return;
  }
  cout << "not coprime"
       << "\\n";
}
int main() {
  io;
  // freopen("in.txt" ,"r" , stdin) ;
  solve();
}