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

/*************************************************************************
    > File Name: solve.cpp
    > Author: XeroxAuto
    > Mail: lanzongwei@gmail.com
    > Created Time: 2020-08-29 20:14:55
 ************************************************************************/

#define GOODOJ
#define SYNC 0

#ifdef GOODOJ
#include <bits/stdc++.h>
#include <chrono>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/priority_queue.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
#include <random>
using namespace __gnu_pbds;
using namespace __gnu_cxx;
#else
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <set>
#include <sstream>
#include <string>
#include <vector>
#endif
using namespace std;

struct InputOutputStream {

  enum { SIZE = 1000001 };

  char ibuf[SIZE], *s, *t, obuf[SIZE], *oh;

  bool eof;

  InputOutputStream() : s(), t(), oh(obuf), eof(false) {}

  ~InputOutputStream() { fwrite(obuf, 1, oh - obuf, stdout); }

  inline char read() {

    if (s == t)
      t = (s = ibuf) + fread(ibuf, 1, SIZE, stdin);

    return s == t ? -1 : *s++;
  }

  inline InputOutputStream &operator>>(char *x) {

    static char c;

    for (c = read(); isspace(c); c = read())

      if (c == -1) {
        eof = true;
        return *this;
      }

    for (; !isspace(c); c = read())
      *x = c, ++x;

    *x = 0;

    return *this;
  }

  template <typename T>

  inline InputOutputStream &operator>>(T &x) {

    static char c;

    static bool iosig;

    for (c = read(), iosig = false; !isdigit(c); c = read()) {

      if (c == -1) {
        eof = true;
        return *this;
      }

      iosig |= c == '-';
    }

    for (x = 0; isdigit(c); c = read())
      x = x * 10 + (c ^ '0');

    if (iosig)
      x = -x;

    return *this;
  }

  inline void print(char c) {

    if (oh == obuf + SIZE) {

      fwrite(obuf, 1, SIZE, stdout);

      oh = obuf;
    }

    *oh++ = c;
  }

  template <typename T>

  inline void print(T x) {

    static int buf[23], cnt;

    if (x != 0) {

      if (x < 0)
        print('-'), x = -x;

      for (cnt = 0; x; x /= 10)
        buf[++cnt] = x % 10 | 48;

      while (cnt)
        print((char)buf[cnt--]);

    } else
      print('0');
  }

  template <typename T>

  inline InputOutputStream &operator<<(const T &x) {

    print(x);

    return *this;
  }

  inline void print(const char *x) {

    for (; *x; x++)

      print(*x);
  }

} io;

#ifndef DEBUG

#define cin io

#define cout io

#endif

#define endl '\\n'
#define fep(i, b, e) for (int i = (b); i < (e); ++i)
#define rep(i, x) for (int i = 0; i < (x); ++i)
#define rap(i, x) for (auto &i : (x))
#define seg(t) (t).begin(), (t).end()
#define ep emplace_back
#define mkp make_pair
#define qxx(i, x) for (int i = head[x]; ~i; i = node[i].nex)
#define F first
#define S second
#define lowbit(x) ((-(x)) & (x))
#define RE register
#define getchar() getchar_unlocked()
#ifdef DEBUG
void err(istream_iterator<string>) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cerr << *it << " = " << a << ' ';
  err(++it, args...);
}
#define debug(args...)                                                         \\
  {                                                                            \\
    string _s = #args;                                                         \\
    replace(seg(_s), ',', ' ');                                                \\
    cerr << "DEBUG:";                                                          \\
    istringstream it(_s);                                                      \\
    err(istream_iterator<string>(it), args);                                   \\
    cerr << endl;                                                              \\
  }
#else
#define debug(...)
#endif

template <typename T> inline bool cmax(T &a, const T &b) {
  return a < b ? a = b, 1 : 0;
}
template <typename T> inline bool cmin(T &a, const T &b) {
  return a > b ? a = b, 1 : 0;
}

#ifdef GOODOJ
mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count());
typedef __gnu_pbds::priority_queue<int> pq;
#endif
typedef std::string str;
typedef long long ll;
typedef double db;
typedef pair<int, int> pa;

const double P = acos(-1.0), eps = 1e-9;
struct point {
  db x, y;
};
inline int sign(db a) { return a < -eps ? -1 : a > eps; }
#define dot(p1, p2, p3)                                                        \\
  ((p2.x - p1.x) * (p3.x - p1.x) + (p2.y - p1.y) * (p3.y - p1.y))
#define cross(p1, p2, p3)                                                      \\
  ((p2.x - p1.x) * (p3.y - p1.y) - (p3.x - p1.x) * (p2.y - p1.y))
#define crossOp(p1, p2, p3) sign(cross(p1, p2, p3))

const int Ma = 1e6 + 1000, inf = 0x3f3f3f3f, mod = 1e9 + 7;

bool is[Ma];
int prim[Ma], id[Ma], mz[Ma];
int cnt;
void dbb() {
  is[0] = is[1] = true;
  for (int i = 2; i < Ma; i++) {
    if (!is[i])
      prim[cnt++] = i, id[i] = cnt - 1, mz[i] = cnt - 1;
    for (int j = 0; j < cnt and prim[j] * i < Ma; j++) {
      is[prim[j] * i] = true;
      mz[prim[j] * i] = j;
      if (i % prim[j] == 0)
        break;
    }
  }
}

int ct[Ma];
bool pr, ck;
int n;

void gen(int tt) {
  if (!pr and !ck)
    return;
  while (tt > 1) {
    debug(mz[tt], tt);
    if (pr and ct[mz[tt]])
      pr = false;
    ++ct[mz[tt]];
    if (ct[mz[tt]] == n)
      ck = false;
    int t = prim[mz[tt]];
    while (tt % t == 0)
      tt /= t;
  }
}

int a[Ma];

signed main() {
#if SYNC == 1
  ios::sync_with_stdio(false);
  cin.tie(0);
#endif
  cin >> n;
  dbb();
  pr = true;
  ck = true;
  rep(i, n) {
    cin >> a[i];
    gen(a[i]);
  }
  if (pr)
    cout << "pairwise coprime" << endl;
  else if (ck)
    cout << "setwise coprime" << endl;
  else
    cout << "not coprime" << endl;

  return 0;
}

Error: TLE

Fixed submission:

/*************************************************************************
    > File Name: solve.cpp
    > Author: XeroxAuto
    > Mail: lanzongwei@gmail.com
    > Created Time: 2020-08-29 20:14:55
 ************************************************************************/

#define GOODOJ
#define SYNC 0

#ifdef GOODOJ
#include <bits/stdc++.h>
#include <chrono>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/priority_queue.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
#include <random>
using namespace __gnu_pbds;
using namespace __gnu_cxx;
#else
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <set>
#include <sstream>
#include <string>
#include <vector>
#endif
using namespace std;

struct InputOutputStream {

  enum { SIZE = 1000001 };

  char ibuf[SIZE], *s, *t, obuf[SIZE], *oh;

  bool eof;

  InputOutputStream() : s(), t(), oh(obuf), eof(false) {}

  ~InputOutputStream() { fwrite(obuf, 1, oh - obuf, stdout); }

  inline char read() {

    if (s == t)
      t = (s = ibuf) + fread(ibuf, 1, SIZE, stdin);

    return s == t ? -1 : *s++;
  }

  inline InputOutputStream &operator>>(char *x) {

    static char c;

    for (c = read(); isspace(c); c = read())

      if (c == -1) {
        eof = true;
        return *this;
      }

    for (; !isspace(c); c = read())
      *x = c, ++x;

    *x = 0;

    return *this;
  }

  template <typename T>

  inline InputOutputStream &operator>>(T &x) {

    static char c;

    static bool iosig;

    for (c = read(), iosig = false; !isdigit(c); c = read()) {

      if (c == -1) {
        eof = true;
        return *this;
      }

      iosig |= c == '-';
    }

    for (x = 0; isdigit(c); c = read())
      x = x * 10 + (c ^ '0');

    if (iosig)
      x = -x;

    return *this;
  }

  inline void print(char c) {

    if (oh == obuf + SIZE) {

      fwrite(obuf, 1, SIZE, stdout);

      oh = obuf;
    }

    *oh++ = c;
  }

  template <typename T>

  inline void print(T x) {

    static int buf[23], cnt;

    if (x != 0) {

      if (x < 0)
        print('-'), x = -x;

      for (cnt = 0; x; x /= 10)
        buf[++cnt] = x % 10 | 48;

      while (cnt)
        print((char)buf[cnt--]);

    } else
      print('0');
  }

  template <typename T>

  inline InputOutputStream &operator<<(const T &x) {

    print(x);

    return *this;
  }

  inline void print(const char *x) {

    for (; *x; x++)

      print(*x);
  }

} io;

#ifndef DEBUG

#define cin io

#define cout io

#endif

#define endl '\\n'
#define fep(i, b, e) for (int i = (b); i < (e); ++i)
#define rep(i, x) for (int i = 0; i < (x); ++i)
#define rap(i, x) for (auto &i : (x))
#define seg(t) (t).begin(), (t).end()
#define ep emplace_back
#define mkp make_pair
#define qxx(i, x) for (int i = head[x]; ~i; i = node[i].nex)
#define F first
#define S second
#define lowbit(x) ((-(x)) & (x))
#define RE register
#define getchar() getchar_unlocked()
#ifdef DEBUG
void err(istream_iterator<string>) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cerr << *it << " = " << a << ' ';
  err(++it, args...);
}
#define debug(args...)                                                         \\
  {                                                                            \\
    string _s = #args;                                                         \\
    replace(seg(_s), ',', ' ');                                                \\
    cerr << "DEBUG:";                                                          \\
    istringstream it(_s);                                                      \\
    err(istream_iterator<string>(it), args);                                   \\
    cerr << endl;                                                              \\
  }
#else
#define debug(...)
#endif

template <typename T> inline bool cmax(T &a, const T &b) {
  return a < b ? a = b, 1 : 0;
}
template <typename T> inline bool cmin(T &a, const T &b) {
  return a > b ? a = b, 1 : 0;
}

#ifdef GOODOJ
mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count());
typedef __gnu_pbds::priority_queue<int> pq;
#endif
typedef std::string str;
typedef long long ll;
typedef double db;
typedef pair<int, int> pa;

const double P = acos(-1.0), eps = 1e-9;
struct point {
  db x, y;
};
inline int sign(db a) { return a < -eps ? -1 : a > eps; }
#define dot(p1, p2, p3)                                                        \\
  ((p2.x - p1.x) * (p3.x - p1.x) + (p2.y - p1.y) * (p3.y - p1.y))
#define cross(p1, p2, p3)                                                      \\
  ((p2.x - p1.x) * (p3.y - p1.y) - (p3.x - p1.x) * (p2.y - p1.y))
#define crossOp(p1, p2, p3) sign(cross(p1, p2, p3))

const int Ma = 1e6 + 1000, inf = 0x3f3f3f3f, mod = 1e9 + 7;

bool is[Ma];
int prim[Ma], id[Ma], mz[Ma];
int cnt;
void dbb() {
  is[0] = is[1] = true;
  for (int i = 2; i < Ma; i++) {
    if (!is[i])
      prim[cnt++] = i, id[i] = cnt - 1, mz[i] = cnt - 1;
    for (int j = 0; j < cnt and prim[j] * i < Ma; j++) {
      is[prim[j] * i] = true;
      if (i % prim[j] == 0) {
        mz[i] = j;
        break;
      }
    }
  }
}

int ct[Ma];
bool pr, ck;
int n;

void gen(int tt) {
  if (!pr and !ck)
    return;
  while (tt > 1) {
    if (pr and ct[mz[tt]])
      pr = false;
    ++ct[mz[tt]];
    if (ct[mz[tt]] == n)
      ck = false;
    int t = prim[mz[tt]];
    while (tt % t == 0)
      tt /= t;
  }
}

int a[Ma];

signed main() {
#if SYNC == 1
  ios::sync_with_stdio(false);
  cin.tie(0);
#endif
  cin >> n;
  dbb();
  pr = true;
  ck = true;
  rep(i, n) {
    cin >> a[i];
    gen(a[i]);
  }
  if (pr)
    cout << "pairwise coprime" << endl;
  else if (ck)
    cout << "setwise coprime" << endl;
  else
    cout << "not coprime" << endl;

  return 0;
}