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

// #pragma GCC optimize("Ofast")
// #pragma GCC target("avx,avx2,fma")
// #pragma GCC optimization ("unroll-loops")
#include <bits/stdc++.h>
#define pb push_back
#define int long long
#define MEM(x) memset(x, 0, sizeof(x))
#define vc vector<int>
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define inf 0x3f3f3f3f
#define t_case                                                                 \\
  int tt;                                                                      \\
  cin >> tt;                                                                   \\
  while (tt--)
#define lnginf 9223372036854775807
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define For(i, j, k) for (int i = (int)(j); i < (int)(k); i++)
#define rFor(i, j, k) for (int i = (int)(k - 1); i >= (int)(j); i--)
#define ins insert
#define in(x)                                                                  \\
  ;                                                                            \\
  {                                                                            \\
    for (auto &i : x)                                                          \\
      cin >> i;                                                                \\
  }
#define out(x)                                                                 \\
  ;                                                                            \\
  {                                                                            \\
    for (auto i : x)                                                           \\
      cout << i << ' ';                                                        \\
  }
#define pi pair<int, int>
#define ti tuple<int, int, int>
#define PI 3.14159265359
#define des(x) setprecision(x)
const long long LL_INF = (long long)2e18 + 5;
using namespace std;
// 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
const int N = 2e5 + 10, mod = 1e9 + 7;
vector<int> div(int a) {
  vector<int> ans;
  for (int i = 2; i * i <= a; i++) {
    if (a % i == 0) {
      ans.pb(i);
      while (a % i == 0)
        a /= i;
    }
  }
  if (a != 1)
    ans.pb(a);
  return ans;
}
void solve() {
  int n;
  cin >> n;
  bool cok = 1;
  vector<int> v(n);
  in(v);
  int g = v[0];
  set<int> now;
  For(i, 0, n) {
    g = __gcd(v[i], g);
    if (!cok)
      continue;
    vector<int> prime = div(v[i]);
    for (auto p : prime) {
      cok &= (!now.count(p));
      now.ins(p);
    }
  }
  if (cok) {
    cout << "pairwise coprime\\n";
    return;
  }
  if (g == 1) {
    cout << "setwise coprime\\n";
    return;
  }
  cout << "not coprime\\n";
}
int32_t main() {

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  fastio
  // t_case

  solve();
}

Error: TLE

Fixed submission:

// #pragma GCC optimize("Ofast")
// #pragma GCC target("avx,avx2,fma")
// #pragma GCC optimization ("unroll-loops")
#include <bits/stdc++.h>
#define pb push_back
#define int long long
#define MEM(x) memset(x, 0, sizeof(x))
#define vc vector<int>
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define inf 0x3f3f3f3f
#define t_case                                                                 \\
  int tt;                                                                      \\
  cin >> tt;                                                                   \\
  while (tt--)
#define lnginf 9223372036854775807
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define For(i, j, k) for (int i = (int)(j); i < (int)(k); i++)
#define rFor(i, j, k) for (int i = (int)(k - 1); i >= (int)(j); i--)
#define ins insert
#define in(x)                                                                  \\
  ;                                                                            \\
  {                                                                            \\
    for (auto &i : x)                                                          \\
      cin >> i;                                                                \\
  }
#define out(x)                                                                 \\
  ;                                                                            \\
  {                                                                            \\
    for (auto i : x)                                                           \\
      cout << i << ' ';                                                        \\
  }
#define pi pair<int, int>
#define ti tuple<int, int, int>
#define PI 3.14159265359
#define des(x) setprecision(x)
const long long LL_INF = (long long)2e18 + 5;
using namespace std;
// 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
const int N = 2e5 + 10, mod = 1e9 + 7;
const int mx = 1e6 + 4;
vector<int> v(mx, 0);
map<int, set<int>> prime;
void sieve() {
  for (int i = 2; i <= mx; i++)
    if (!v[i]) {
      for (int j = i; j <= mx; j += i) {
        prime[j].ins(i);
        v[j] = 1;
      }
    }
}
void solve() {
  int n;
  cin >> n;
  bool cok = 1;
  vector<int> v(n);
  in(v);
  int g;
  set<int> now;
  rFor(i, 0, n) {
    if (i == n - 1)
      g = v[i];
    else
      g = __gcd(v[i], g);
    if (!cok)
      continue;
    for (auto p : prime[v[i]]) {
      debug(p);
      if (i != n - 1)
        cok &= (!now.count(p));
      now.ins(p);
    }
  }
  if (cok) {
    cout << "pairwise coprime\\n";
    return;
  }
  if (g == 1) {
    cout << "setwise coprime\\n";
    return;
  }
  cout << "not coprime\\n";
}
int32_t main() {

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  fastio
  // t_case
  sieve();
  solve();
}