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

/*
  ╔╗         ╔╗              ╔═══╗
  ║║        ╔╝╚╗             ║╔═╗║
  ║║╔╗╔╗╔══╗╚╗╔╝    ╔══╗     ║╚═╝║╔═╗╔══╗╔══╗╔═╗╔══╗ ╔╗╔╗╔╗╔╗╔══╗╔═╗
╔╗║║║║║║║══╣ ║║     ╚ ╗║     ║╔══╝║╔╝║╔╗║║╔╗║║╔╝╚ ╗║ ║╚╝║║╚╝║║╔╗║║╔╝
║╚╝║║╚╝║╠══║ ║╚╗    ║╚╝╚╗    ║║   ║║ ║╚╝║║╚╝║║║ ║╚╝╚╗║║║║║║║║║║═╣║║
╚══╝╚══╝╚══╝ ╚═╝    ╚═══╝    ╚╝   ╚╝ ╚══╝╚═╗║╚╝ ╚═══╝╚╩╩╝╚╩╩╝╚══╝╚╝
                                         ╔═╝║
                                         ╚══╝                       */

// Header files
#include <bits/stdc++.h>
using namespace std;
using namespace std::chrono;

/*-----------------------------------------------Debug Template
 * Start-----------------------------------------------*/
void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\\'' << x << '\\''; }
void __print(const char *x) { cerr << '\\"' << x << '\\"'; }
void __print(const string &x) { cerr << '\\"' << x << '\\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }

template <typename T, typename V> void __print(const pair<T, V> &x) {
  cerr << '{';
  __print(x.first);
  cerr << ',';
  __print(x.second);
  cerr << '}';
}
template <typename T> void __print(const T &x) {
  int f = 0;
  cerr << '{';
  for (auto &i : x)
    cerr << (f++ ? "," : ""), __print(i);
  cerr << "}";
}
void _print() { cerr << "]\\n"; }
template <typename T, typename... V> void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v))
    cerr << ", ";
  _print(v...);
}
#ifndef ONLINE_JUDGE
#define debug(x...)                                                            \\
  cerr << "[" << #x << "] = [";                                                \\
  _print(x)
#else
#define debug(x...)
#endif

/*-----------------------------------------------Debug Template
 * End-----------------------------------------------*/

// Short data-types declaration
typedef long long ll;
typedef vector<int> vi;
typedef vector<long long> vl;
typedef vector<string> vs;
typedef vector<vector<int>> vii;
typedef vector<vector<long long>> vll;
typedef vector<bool> vb;
typedef vector<vector<bool>> vbb;

// Macros
#define MK make_pair
#define EB emplace_back
#define PB push_back
#define F first
#define S second
#define rep(i, start, end) for (int i = (int)start; i < (int)end; i++)
#define repr(i, start, end) for (int i = (int)start; i > (int)end; i--)
#define all(x) (x).begin(), (x).end()

// Constants
const int mod = 1e9 + 7;

// Some useful functions
ll binpow(ll num, ll pow);
inline int mul(int a, int b) { return (a * 1ll * b) % mod; }
inline int add(int a, int b) { return (a + 0ll + b) % mod; }
inline int sub(int a, int b) { return ((a + 0ll - b) % mod + mod) % mod; }
/*------------------------------------------------------Code
 * Starts------------------------------------------------------*/
inline void solve() {
  int n;
  cin >> n;
  vi a(n);
  rep(i, 0, n) cin >> a[i];
  set<int> f;
  bool flag = true;
  int g = a[n - 1];
  repr(i, n - 1, -1) {
    int num = a[i];
    g = __gcd(g, num);
    for (int i = 2; i * 1ll * i <= num; i++) {
      if (num % i == 0) {
        if (f.count(i))
          flag = false;
        while (num % i == 0)
          num /= i;
        f.insert(i);
      }
    }
    if (num > 1) {
      if (f.count(num))
        flag = false;
      f.insert(num);
    }
  }
  if (flag)
    cout << "pairwise coprime\\n";
  else if (g == 1)
    cout << "setwise coprime\\n";
  else
    cout << "not coprime\\n";
}

int main() {
  auto start = high_resolution_clock::now();
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  int t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }
  auto stop = high_resolution_clock::now();
  auto duration = duration_cast<microseconds>(stop - start);
  cerr << duration.count() << " microseconds\\n";
  return 0;
}
/*------------------------------------------------------Code
 * ends------------------------------------------------------*/
ll binpow(ll num, ll pow) {
  if (pow == 0)
    return 1;
  ll val = binpow(num, pow / 2);
  val *= val;
  if (pow & 1)
    val *= num;
  return val;
}


Error: TLE

Fixed submission:

/*
  ╔╗         ╔╗              ╔═══╗
  ║║        ╔╝╚╗             ║╔═╗║
  ║║╔╗╔╗╔══╗╚╗╔╝    ╔══╗     ║╚═╝║╔═╗╔══╗╔══╗╔═╗╔══╗ ╔╗╔╗╔╗╔╗╔══╗╔═╗
╔╗║║║║║║║══╣ ║║     ╚ ╗║     ║╔══╝║╔╝║╔╗║║╔╗║║╔╝╚ ╗║ ║╚╝║║╚╝║║╔╗║║╔╝
║╚╝║║╚╝║╠══║ ║╚╗    ║╚╝╚╗    ║║   ║║ ║╚╝║║╚╝║║║ ║╚╝╚╗║║║║║║║║║║═╣║║
╚══╝╚══╝╚══╝ ╚═╝    ╚═══╝    ╚╝   ╚╝ ╚══╝╚═╗║╚╝ ╚═══╝╚╩╩╝╚╩╩╝╚══╝╚╝
                                         ╔═╝║
                                         ╚══╝                       */

// Header files
#include <bits/stdc++.h>
using namespace std;
using namespace std::chrono;

/*-----------------------------------------------Debug Template
 * Start-----------------------------------------------*/
void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\\'' << x << '\\''; }
void __print(const char *x) { cerr << '\\"' << x << '\\"'; }
void __print(const string &x) { cerr << '\\"' << x << '\\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }

template <typename T, typename V> void __print(const pair<T, V> &x) {
  cerr << '{';
  __print(x.first);
  cerr << ',';
  __print(x.second);
  cerr << '}';
}
template <typename T> void __print(const T &x) {
  int f = 0;
  cerr << '{';
  for (auto &i : x)
    cerr << (f++ ? "," : ""), __print(i);
  cerr << "}";
}
void _print() { cerr << "]\\n"; }
template <typename T, typename... V> void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v))
    cerr << ", ";
  _print(v...);
}
#ifndef ONLINE_JUDGE
#define debug(x...)                                                            \\
  cerr << "[" << #x << "] = [";                                                \\
  _print(x)
#else
#define debug(x...)
#endif

/*-----------------------------------------------Debug Template
 * End-----------------------------------------------*/

// Short data-types declaration
typedef long long ll;
typedef vector<int> vi;
typedef vector<long long> vl;
typedef vector<string> vs;
typedef vector<vector<int>> vii;
typedef vector<vector<long long>> vll;
typedef vector<bool> vb;
typedef vector<vector<bool>> vbb;

// Macros
#define MK make_pair
#define EB emplace_back
#define PB push_back
#define F first
#define S second
#define rep(i, start, end) for (int i = (int)start; i < (int)end; i++)
#define repr(i, start, end) for (int i = (int)start; i > (int)end; i--)
#define all(x) (x).begin(), (x).end()

// Constants
const int mod = 1e9 + 7;

// Some useful functions
ll binpow(ll num, ll pow);
inline int mul(int a, int b) { return (a * 1ll * b) % mod; }
inline int add(int a, int b) { return (a + 0ll + b) % mod; }
inline int sub(int a, int b) { return ((a + 0ll - b) % mod + mod) % mod; }
/*------------------------------------------------------Code
 * Starts------------------------------------------------------*/
inline void solve() {
  int n;
  cin >> n;
  vl a(n);
  rep(i, 0, n) cin >> a[i];
  set<ll> f;
  bool flag = true;
  ll g = a[n - 1];
  repr(i, n - 1, -1) {
    ll num = a[i];
    g = __gcd(g, num);
    for (ll i = 2; i * 1ll * i <= num; i++) {
      if (num % i == 0) {
        if (f.count(i))
          flag = false;
        while (num % i == 0)
          num /= i;
        f.insert(i);
      }
    }
    if (num > 1) {
      if (f.count(num))
        flag = false;
      f.insert(num);
    }
  }
  if (flag)
    cout << "pairwise coprime\\n";
  else if (g == 1)
    cout << "setwise coprime\\n";
  else
    cout << "not coprime\\n";
}

int main() {
  auto start = high_resolution_clock::now();
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  int t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }
  auto stop = high_resolution_clock::now();
  auto duration = duration_cast<microseconds>(stop - start);
  cerr << duration.count() << " microseconds\\n";
  return 0;
}
/*------------------------------------------------------Code
 * ends------------------------------------------------------*/
ll binpow(ll num, ll pow) {
  if (pow == 0)
    return 1;
  ll val = binpow(num, pow / 2);
  val *= val;
  if (pow & 1)
    val *= num;
  return val;
}