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

#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

// ---------- PBDS -----------------

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
#include <ext/pb_ds/tree_policy.hpp>

// ----------------------------------

using namespace std;
using namespace __gnu_pbds;

#define MEM(a, b) memset(a, (b), sizeof(a))
#define ALL(a) a.begin(), a.end()
#define MP(a, b) make_pair(a, b)
#define PB(a) push_back(a)
#define EB(a) emplace_back(a)
#define boost                                                                  \\
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0),                       \\
      cout << fixed << setprecision(10);
// #define ll long long int
// #define int long long int

#define sim template <class c
#define ris return *this
#define dor > debug &operator<<
#define eni(x)                                                                 \\
  sim > typename enable_if<sizeof dud<c>(0) x 1, debug &>::type operator<<(    \\
            c i) {
sim > struct rge {
  c b, e;
};
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c *x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
  ~debug() { cerr << endl; }
  eni(!=) cerr << boolalpha << i;
  ris;
} eni(==) ris << range(begin(i), end(i));
}
sim, class b dor(pair<b, c> d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (auto it = d.b; it != d.e; ++it)
    *this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
#else
  sim dor(const c &) { ris; }
#endif
}
;
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
// debug & operator << (debug & dd, P p) { dd << "(" << p.x << ", " << p.y <<
// ")"; return dd; }
#define deb(x) debug() << imie(x)

int const N = 1e6 + 5;

void solve() {

  int spf[N];

  MEM(spf, 0);

  for (int i = 2; i < N; i++) {
    if (spf[i] == 0) {
      for (int j = i; j < N; j += i)
        spf[j] = i;
    }
  }

  int n;
  cin >> n;

  if (n == 1) {
    cout << "pairwise coprime";
    return;
  }

  int cou[N];

  MEM(cou, 0);
  cou[1] = 1;

  for (int i = 0; i < n; ++i) {
    int x;
    cin >> x;

    while (x != 1) {

      cou[spf[x]]++;
      int num = spf[x];
      while (x % num == 0)
        x /= num;
    }
  }

  int max_num = *max_element(cou, cou + N);

  if (max_num == 1) {
    cout << "pairwise coprime";
  } else if (max_num != n) {
    cout << "setwise coprime";
  } else {
    cout << "not coprime";
  }
}

int32_t main() {

  boost;

  int T;

  // cin >> T;
  T = 1;

  for (int i = 1; i <= T; ++i) {
    solve();
    cout << "\\n";
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

// ---------- PBDS -----------------

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
#include <ext/pb_ds/tree_policy.hpp>

// ----------------------------------

using namespace std;
using namespace __gnu_pbds;

#define MEM(a, b) memset(a, (b), sizeof(a))
#define ALL(a) a.begin(), a.end()
#define MP(a, b) make_pair(a, b)
#define PB(a) push_back(a)
#define EB(a) emplace_back(a)
#define boost                                                                  \\
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0),                       \\
      cout << fixed << setprecision(10);
// #define ll long long int
#define int long long int

#define sim template <class c
#define ris return *this
#define dor > debug &operator<<
#define eni(x)                                                                 \\
  sim > typename enable_if<sizeof dud<c>(0) x 1, debug &>::type operator<<(    \\
            c i) {
sim > struct rge {
  c b, e;
};
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c *x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
  ~debug() { cerr << endl; }
  eni(!=) cerr << boolalpha << i;
  ris;
} eni(==) ris << range(begin(i), end(i));
}
sim, class b dor(pair<b, c> d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (auto it = d.b; it != d.e; ++it)
    *this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
#else
  sim dor(const c &) { ris; }
#endif
}
;
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
// debug & operator << (debug & dd, P p) { dd << "(" << p.x << ", " << p.y <<
// ")"; return dd; }
#define deb(x) debug() << imie(x)

const int MOD = 1e9 + 7;

int parent(vector<int> &dsu, int xx) {

  if (xx != dsu[xx]) {
    dsu[xx] = parent(dsu, dsu[xx]);
  }

  return dsu[xx];
}

void init(vector<int> &prime_list, int N) {

  vector<bool> p(N, 0);

  for (int i = 2; i < N; ++i) {
    if (p[i] == 0) {
      prime_list.push_back(i);
      for (int j = i + i; j < N; j += i) {
        p[i] = 1;
      }
    }
  }
}

const int N = 1e6 + 5;

void add_sum(vector<int> &sum, vector<int> &prime, int n) {
  int p_size = prime.size();
  for (int i = 0; i < p_size; ++i) {
    if (n % prime[i] == 0) {
      while (n % prime[i] == 0) {
        n /= prime[i];
      }
      sum[prime[i]]++;
    }
  }
}

void solve() {

  vector<int> prime;

  init(prime, N);

  int n;
  cin >> n;

  vector<int> v(n);

  for (int i = 0; i < n; ++i) {
    cin >> v[i];
  }

  vector<int> sum(N, 0);

  for (int i = 0; i < n; ++i) {
    add_sum(sum, prime, v[i]);
  }

  bool flag = true;
  bool flag2 = true;

  int p_size = prime.size();

  for (int i = 0; i < p_size; ++i) {
    if (sum[prime[i]] > 1) {
      flag = false;
    }

    if (sum[prime[i]] == n) {
      flag2 = false;
    }
  }

  if (flag) {
    cout << "pairwise coprime";
  } else if (flag2) {
    cout << "setwise coprime";
  } else {
    cout << "not coprime";
  }
}

int32_t main() {

  boost;

  int T;

  // cin >> T;
  T = 1;

  for (int i = 1; i <= T; ++i) {
    solve();
    cout << "\\n";
  }

  return 0;
}