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

#include <algorithm>
#include <boost/multiprecision/cpp_int.hpp>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <string.h>
#include <string>
#include <utility>
#include <vector>

#define rep(i, n) for (int i = 0; i < n; ++i)
#define REP(i, m, n) for (int i = m; i < n; ++i)
#define rrep(i, n) for (int i = n; i >= 0; --i)
#define RREP(i, m, n) for (int i = n; i >= m; --i)
#define llrep(i, n) for (ll i = 0; i < n; ++i)
#define llREP(i, m, n) for (ll i = m; i < n; ++i)
#define llrrep(i, n) for (ll i = n; i >= 0; --i)
#define llRREP(i, m, n) for (ll i = n; i >= m; --i)

using ll = long long;
using ull = unsigned long long;
using pii = std::pair<int, int>;
using pll = std::pair<long long, long long>;
using pil = std::pair<int, long long>;
using pli = std::pair<long long, int>;
using plpii = std::pair<long long, std::pair<int, int>>;
using pdd = std::pair<double, double>;
using pis = std::pair<int, std::string>;

const long long INF = 1000000000000000001;
const long long _MOD = 1000000007;
const long long MOD = 998244353;
const int ALPHABET = 27;
const double pi = 3.14159265358979;

using namespace std;
namespace mp = boost::multiprecision;

template <typename T> constexpr T _gcd(T m, T n) noexcept {
  T g = 1;

  while (m != 0) {
    if (m < n)
      std::swap(m, n);

    if (m & 1) {
      if (n & 1)
        m = (m - n) >> 1;
      else
        n = n >> 1;
    } else {
      if (n & 1) {
        m = m >> 1;
      } else {
        n = n >> 1;
        m = m >> 1;
        g = g << 1;
      }
    }
  }

  return g * n;
}

template <typename T> constexpr T _lcm(T m, T n) noexcept {
  return (m * n) / gcd(m, n);
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n;
  cin >> n;
  ll *a = new ll[n];
  rep(i, n) cin >> a[i];

  ll maxA = a[0];
  ll t = a[0];
  REP(i, 1, n) {
    t = _gcd(t, a[i]);
    maxA = max(maxA, a[i]);
  }

  if (t == 1) {
    ll *d = new ll[maxA + 1];
    memset(d, 0, sizeof(ll) * (maxA + 1));
    d[1] = 1;
    REP(i, 2, maxA + 1) {
      if (d[i] == 0) {
        d[i] = i;
        for (int j = 2; i * j < maxA + 1; ++j) {
          d[i * j] = i;
        }
      }
    }

    set<ll> x;
    while (a[0] != 1) {
      x.insert(d[a[0]]);
      a[0] /= d[a[0]];
    }

    REP(i, 1, n) {
      while (a[i] != 1) {
        if (x.find(d[a[i]]) != x.end()) {
          cout << "setwise coprime"
               << "\\n";
          return 0;
        } else {
          x.insert(d[a[i]]);
          ll p = d[a[i]];
          while (a[i] % p == 0) {
            a[i] /= p;
          }
        }
      }
    }

    cout << "pairwise coprime"
         << "\\n";
  } else {
    cout << "not coprime"
         << "\\n";
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <boost/multiprecision/cpp_int.hpp>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <string.h>
#include <string>
#include <utility>
#include <vector>

#define rep(i, n) for (int i = 0; i < n; ++i)
#define REP(i, m, n) for (int i = m; i < n; ++i)
#define rrep(i, n) for (int i = n; i >= 0; --i)
#define RREP(i, m, n) for (int i = n; i >= m; --i)
#define llrep(i, n) for (ll i = 0; i < n; ++i)
#define llREP(i, m, n) for (ll i = m; i < n; ++i)
#define llrrep(i, n) for (ll i = n; i >= 0; --i)
#define llRREP(i, m, n) for (ll i = n; i >= m; --i)

using ll = long long;
using ull = unsigned long long;
using pii = std::pair<int, int>;
using pll = std::pair<long long, long long>;
using pil = std::pair<int, long long>;
using pli = std::pair<long long, int>;
using plpii = std::pair<long long, std::pair<int, int>>;
using pdd = std::pair<double, double>;
using pis = std::pair<int, std::string>;

const long long INF = 1000000000000000001;
const long long _MOD = 1000000007;
const long long MOD = 998244353;
const int ALPHABET = 27;
const double pi = 3.14159265358979;

using namespace std;
namespace mp = boost::multiprecision;

template <typename T> constexpr T _gcd(T m, T n) noexcept {
  T g = 1;

  while (m != 0) {
    if (m < n)
      std::swap(m, n);

    if (m & 1) {
      if (n & 1)
        m = (m - n) >> 1;
      else
        n = n >> 1;
    } else {
      if (n & 1) {
        m = m >> 1;
      } else {
        n = n >> 1;
        m = m >> 1;
        g = g << 1;
      }
    }
  }

  return g * n;
}

template <typename T> constexpr T _lcm(T m, T n) noexcept {
  return (m * n) / gcd(m, n);
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n;
  cin >> n;
  ll *a = new ll[n];
  rep(i, n) cin >> a[i];

  ll t = a[0];
  REP(i, 1, n) { t = _gcd(t, a[i]); }

  if (t == 1) {
    mp::cpp_int p(a[0] * a[1]);
    REP(i, 2, n) {
      if (_gcd(p, static_cast<mp::cpp_int>(a[i])) != 1) {
        cout << "setwise coprime"
             << "\\n";
        return 0;
      }
      p *= static_cast<mp::cpp_int>(a[i]);
    }

    cout << "pairwise coprime"
         << "\\n";
  } else {
    cout << "not coprime"
         << "\\n";
  }

  return 0;
}