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

// =====================================================
//   File    :   main.cpp
//   Author  :   eezyl
//   Synopsis:   codeforces/atcoder template
//   Address :
//   Date    :   2020/08/28
// =====================================================
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef double db;
typedef string str;

typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef pair<db, db> pd;

typedef vector<int> vi;
typedef vector<bool> vb;
typedef vector<ll> vll;
typedef vector<db> vd;
typedef vector<str> vs;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<pd> vpd;
typedef vector<vi> vvi;

#define mp make_pair
#define fi first
#define se second
#define sz(x) (int)(x).size()
#define all(x) begin(x), end(x)
#define rall(x) (x).rbegin(), (x).rend()
#define sor(x) sort(all(x))
#define rsz resize
#define ins insert
#define ft front()
#define bk back()
#define pf push_front
#define pb push_back
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define F0R(i, a) FOR(i, 0, a)
#define ROF(i, a, b) for (int i = (b)-1; i >= (a); --i)
#define R0F(i, a) ROF(i, 0, a)
#define trav(a, x) for (auto &a : x)

const ll INF = 1e18;
const ld PI = acos((ld)-1);
const int d4i[4] = {-1, 0, 1, 0}, d4j[4] = {0, 1, 0, -1};
const int d8i[8] = {-1, -1, 0, 1, 1, 1, 0, -1},
          d8j[8] = {0, 1, 1, 1, 0, -1, -1, -1};

// TO_STRING
#define ts to_string
str ts(char c) { return str(1, c); }
str ts(const char *s) { return (str)s; }
str ts(str s) { return s; }
str ts(bool b) { return b ? "true" : "false"; }
str ts(vector<bool> v) {
  str res;
  F0R(i, sz(v)) res += char('0' + v[i]);
  return res;
}
template <size_t SZ> str ts(bitset<SZ> b) {
  str res;
  F0R(i, SZ) res += char('0' + b[i]);
  return res;
}

template <class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
template <class T> bool ckmin(T &a, const T &b) { return b < a ? a = b, 1 : 0; }
template <class T> bool ckmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; }
constexpr int pct(int x) { return __builtin_popcount(x); }
constexpr int bits(int x) { return 31 - __builtin_clz(x); } // floor(log2(x))

const ll MOD = 1e9 + 7; // 998244353;
const int MX = 1e6 + 5;

int tree[1];

int sum(int k) {
  int a = 0;
  while (k >= 1) {
    a += tree[k];
    k -= k & -k;
  }
  return a;
}

void add(int k, int x) {
  while (k <= MX) {
    tree[k] += x;
    k += k & -k;
  }
}

int table[MX] = {0};

void solve() {
  vi prime1001;

  for (int i = 2; i < 1001; ++i)
    if (table[i] == 0) {
      prime1001.pb(i);
      for (int j = i; j < MX; j += i)
        table[j] = 1;
    }

  int n;
  cin >> n;
  vi A(n);
  F0R(i, n)
  cin >> A[i];

  int gcdall = A[0];
  bool flag = 0;
  set<int> st;
  F0R(i, n) {
    gcdall = gcd(gcdall, A[i]);
    // cout << gcdall << endl;
    for (auto p : prime1001)
      if (A[i] % p == 0) {
        if (st.find(p) != st.end())
          flag = 1;
        else {
          st.insert(p);
          while (A[i] % p == 0)
            A[i] /= p;
        }
      }
    if (A[i] != 1) {
      if (st.find(A[i]) != st.end())
        flag = 1;
      else
        st.insert(A[i]);
    }
  }
  if (gcdall != 1) {
    cout << "not coprime\\n";
  } else if (flag) {
    cout << "setwise coprime\\n";
  } else {
    cout << "pairwise coprime\\n";
  }
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
// #define LOCAL
#ifdef LOCAL
  freopen("data.in", "r", stdin);
  while (!feof(stdin)) {
#endif // LOCAL

    int t = 1;
    // cin >> t;
    while (t--) {
      solve();
    }

#ifdef LOCAL
  }
  printf("Time used = %.2lf\\n", (double)clock() / CLOCKS_PER_SEC);
#endif // LOCAL
  return 0;
}


Error: TLE

Fixed submission:

// =====================================================
//   File    :   main.cpp
//   Author  :   eezyl
//   Synopsis:   codeforces/atcoder template
//   Address :
//   Date    :   2020/08/28
// =====================================================
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef double db;
typedef string str;

typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef pair<db, db> pd;

typedef vector<int> vi;
typedef vector<bool> vb;
typedef vector<ll> vll;
typedef vector<db> vd;
typedef vector<str> vs;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<pd> vpd;
typedef vector<vi> vvi;

#define mp make_pair
#define fi first
#define se second
#define sz(x) (int)(x).size()
#define all(x) begin(x), end(x)
#define rall(x) (x).rbegin(), (x).rend()
#define sor(x) sort(all(x))
#define rsz resize
#define ins insert
#define ft front()
#define bk back()
#define pf push_front
#define pb push_back
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define F0R(i, a) FOR(i, 0, a)
#define ROF(i, a, b) for (int i = (b)-1; i >= (a); --i)
#define R0F(i, a) ROF(i, 0, a)
#define trav(a, x) for (auto &a : x)

const ll INF = 1e18;
const ld PI = acos((ld)-1);
const int d4i[4] = {-1, 0, 1, 0}, d4j[4] = {0, 1, 0, -1};
const int d8i[8] = {-1, -1, 0, 1, 1, 1, 0, -1},
          d8j[8] = {0, 1, 1, 1, 0, -1, -1, -1};

// TO_STRING
#define ts to_string
str ts(char c) { return str(1, c); }
str ts(const char *s) { return (str)s; }
str ts(str s) { return s; }
str ts(bool b) { return b ? "true" : "false"; }
str ts(vector<bool> v) {
  str res;
  F0R(i, sz(v)) res += char('0' + v[i]);
  return res;
}
template <size_t SZ> str ts(bitset<SZ> b) {
  str res;
  F0R(i, SZ) res += char('0' + b[i]);
  return res;
}

template <class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
template <class T> bool ckmin(T &a, const T &b) { return b < a ? a = b, 1 : 0; }
template <class T> bool ckmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; }
constexpr int pct(int x) { return __builtin_popcount(x); }
constexpr int bits(int x) { return 31 - __builtin_clz(x); } // floor(log2(x))

const ll MOD = 1e9 + 7; // 998244353;
const int MX = 1e6 + 5;

int tree[1];

int sum(int k) {
  int a = 0;
  while (k >= 1) {
    a += tree[k];
    k -= k & -k;
  }
  return a;
}

void add(int k, int x) {
  while (k <= MX) {
    tree[k] += x;
    k += k & -k;
  }
}

int table[MX] = {0};

void solve() {
  vi prime1001;

  for (int i = 2; i < 1001; ++i)
    if (table[i] == 0) {
      prime1001.pb(i);
      for (int j = i; j < MX; j += i)
        table[j] = 1;
    }
  for (int i = 1001; i < MX; ++i)
    if (table[i] == 0)
      prime1001.pb(i);

  int n;
  cin >> n;
  vi A(n);
  F0R(i, n)
  cin >> A[i];

  int gcdall = A[0];
  bool flag = 0;
  set<int> st;
  F0R(i, n) {
    gcdall = gcd(gcdall, A[i]);
    // cout << gcdall << endl;
    for (auto p : prime1001)
      if (A[i] % p == 0) {
        if (st.find(p) != st.end())
          flag = 1;
        else {
          st.insert(p);
        }
      }
  }
  if (gcdall != 1) {
    cout << "not coprime\\n";
  } else if (flag) {
    cout << "setwise coprime\\n";
  } else {
    cout << "pairwise coprime\\n";
  }
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
// #define LOCAL
#ifdef LOCAL
  freopen("data.in", "r", stdin);
  while (!feof(stdin)) {
#endif // LOCAL

    int t = 1;
    // cin >> t;
    while (t--) {
      solve();
    }

#ifdef LOCAL
  }
  printf("Time used = %.2lf\\n", (double)clock() / CLOCKS_PER_SEC);
#endif // LOCAL
  return 0;
}