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

#include <bits/stdc++.h>
using namespace std;
typedef long long int llint;
typedef unsigned long long int ullint;
std::string YesNo[] = {"No", "Yes"};
#define loop(i, N) for (int i = 0; i < (N); i++)
template <typename T> auto sort_all(T &v) {
  return std::sort(v.begin(), v.end());
}
template <typename T> auto sum_all(const T &v) {
  auto z = v[0];
  z = 0;
  return std::accumulate(v.begin(), v.end(), z);
}
template <typename T> auto &max(T &v) {
  return *std::max_element(v.begin(), v.end());
}
template <typename T>
auto input(std::size_t N, const std::vector<T> &head = {},
           const std::vector<T> &tail = {}) {
  auto s = head.size();
  auto t = tail.size();
  std::vector<T> v;
  v.reserve(s + N + t);
  v.insert(v.end(), head.begin(), head.end());
  v.resize(s + N);
  loop(i, N) std::cin >> v[s + i];
  v.insert(v.end(), tail.begin(), tail.end());
  return std::move(v);
}
class range {
  typedef int Int;
  Int start, stop, step;
  void initialize() {
    auto sign = (step >= 0) ? 1 : -1;
    stop = start + ((stop - start - sign) / step + 1) * step;
    if ((stop - start) * step <= 0)
      stop = start;
  }

public:
  range(Int start, Int stop, Int step = 1)
      : start(start), stop(stop), step(step) {
    initialize();
  }
  range(Int stop) : start(0), stop(stop), step(1) { initialize(); }
  struct iterator {
    Int i, step;
    Int &operator*() { return i; }
    iterator &operator++() {
      i += step;
      return *this;
    }
    bool operator!=(const iterator &v) { return i != v.i; }
  };
  iterator begin() { return {start, step}; }
  iterator end() { return {stop, step}; }
};
template <typename T1, typename T2>
std::pair<T1, T2> operator-(const std::pair<T1, T2> &x) {
  return std::make_pair(-x.first, -x.second);
}
template <typename T1, typename T2>
std::pair<T1, T2> operator+(const std::pair<T1, T2> &x,
                            const std::pair<T1, T2> &y) {
  return std::make_pair(x.first + y.first, x.second + y.second);
}
template <typename T1, typename T2>
std::pair<T1, T2> operator-(const std::pair<T1, T2> &x,
                            const std::pair<T1, T2> &y) {
  return x + (-y);
}
template <typename T1, typename T2>
std::pair<T1, T2> &operator+=(std::pair<T1, T2> &x,
                              const std::pair<T1, T2> &y) {
  x.first += y.first;
  x.second += y.second;
  return x;
}
template <typename T1, typename T2>
std::pair<T1, T2> &operator-=(std::pair<T1, T2> &x,
                              const std::pair<T1, T2> &y) {
  return x += (-y);
}
template <typename T1, typename T2>
std::ostream &operator<<(std::ostream &cout, const std::pair<T1, T2> &data) {
  cout << "(" << data.first << ", " << data.second << ")";
  return cout;
}
template <typename T>
std::ostream &operator<<(std::ostream &cout, const std::vector<T> &data) {
  cout << "[";
  if (data.size() == 0) {
  } else if (data.size() == 1) {
    cout << data[0];
  } else {
    cout << data[0];
    for (auto itr = data.begin() + 1; itr != data.end(); ++itr) {
      cout << ", " << *itr;
    }
  }
  cout << "]";
  return cout;
}

typedef llint Int;

template <typename T> auto gcd(T a, T b) {
  while (b > 0) {
    T tmp = a % b;
    a = b;
    b = tmp;
  }
  return a;
}

template <typename T> auto primes(T N) {
  vector<T> P(0);
  for (auto i : range(2, N)) {
    int sieve = 1;
    for (auto p : P) {
      if (p * p > i) {
        break;
      } else if (i % p == 0) {
        sieve = 0;
        break;
      }
    }
    if (sieve == 0) {
      continue;
    } else {
      P.push_back(i);
    }
  }
  return P;
}

int main(void) {
  int N;
  cin >> N;
  auto A = input<int>(N);

  auto g = A[0];
  for (auto a : A) {
    g = gcd(g, a);
  }
  if (g > 1) {
    cout << "not coprime" << endl;
    return 0;
  }

  auto P = primes<int>(1000);
  for (auto p : P) {
    int n = 0;
    for (auto &a : A) {
      if (a % p == 0) {
        n += 1;
        while (a % p == 0) {
          a /= p;
        }
        if (n > 1) {
          cout << "setwise coprime" << endl;
          return 0;
        }
      }
    }
  }
  set<int> st;
  for (auto a : A) {
    if (a == 1) {
      continue;
    }
    if (st.count(a)) {
      cout << "setwise coprime" << endl;
      return 0;
    } else {
      st.insert(a);
    }
  }
  cout << "pairwise coprime" << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long int llint;
typedef unsigned long long int ullint;
std::string YesNo[] = {"No", "Yes"};
#define loop(i, N) for (int i = 0; i < (N); i++)
template <typename T> auto sort_all(T &v) {
  return std::sort(v.begin(), v.end());
}
template <typename T> auto sum_all(const T &v) {
  auto z = v[0];
  z = 0;
  return std::accumulate(v.begin(), v.end(), z);
}
template <typename T> auto &max(T &v) {
  return *std::max_element(v.begin(), v.end());
}
template <typename T>
auto input(std::size_t N, const std::vector<T> &head = {},
           const std::vector<T> &tail = {}) {
  auto s = head.size();
  auto t = tail.size();
  std::vector<T> v;
  v.reserve(s + N + t);
  v.insert(v.end(), head.begin(), head.end());
  v.resize(s + N);
  loop(i, N) std::cin >> v[s + i];
  v.insert(v.end(), tail.begin(), tail.end());
  return std::move(v);
}
class range {
  typedef int Int;
  Int start, stop, step;
  void initialize() {
    auto sign = (step >= 0) ? 1 : -1;
    stop = start + ((stop - start - sign) / step + 1) * step;
    if ((stop - start) * step <= 0)
      stop = start;
  }

public:
  range(Int start, Int stop, Int step = 1)
      : start(start), stop(stop), step(step) {
    initialize();
  }
  range(Int stop) : start(0), stop(stop), step(1) { initialize(); }
  struct iterator {
    Int i, step;
    Int &operator*() { return i; }
    iterator &operator++() {
      i += step;
      return *this;
    }
    bool operator!=(const iterator &v) { return i != v.i; }
  };
  iterator begin() { return {start, step}; }
  iterator end() { return {stop, step}; }
};
template <typename T1, typename T2>
std::pair<T1, T2> operator-(const std::pair<T1, T2> &x) {
  return std::make_pair(-x.first, -x.second);
}
template <typename T1, typename T2>
std::pair<T1, T2> operator+(const std::pair<T1, T2> &x,
                            const std::pair<T1, T2> &y) {
  return std::make_pair(x.first + y.first, x.second + y.second);
}
template <typename T1, typename T2>
std::pair<T1, T2> operator-(const std::pair<T1, T2> &x,
                            const std::pair<T1, T2> &y) {
  return x + (-y);
}
template <typename T1, typename T2>
std::pair<T1, T2> &operator+=(std::pair<T1, T2> &x,
                              const std::pair<T1, T2> &y) {
  x.first += y.first;
  x.second += y.second;
  return x;
}
template <typename T1, typename T2>
std::pair<T1, T2> &operator-=(std::pair<T1, T2> &x,
                              const std::pair<T1, T2> &y) {
  return x += (-y);
}
template <typename T1, typename T2>
std::ostream &operator<<(std::ostream &cout, const std::pair<T1, T2> &data) {
  cout << "(" << data.first << ", " << data.second << ")";
  return cout;
}
template <typename T>
std::ostream &operator<<(std::ostream &cout, const std::vector<T> &data) {
  cout << "[";
  if (data.size() == 0) {
  } else if (data.size() == 1) {
    cout << data[0];
  } else {
    cout << data[0];
    for (auto itr = data.begin() + 1; itr != data.end(); ++itr) {
      cout << ", " << *itr;
    }
  }
  cout << "]";
  return cout;
}

typedef llint Int;

template <typename T> auto gcd(T a, T b) {
  while (b > 0) {
    T tmp = a % b;
    a = b;
    b = tmp;
  }
  return a;
}

template <typename T> auto primes(T N) {
  vector<T> P(0);
  for (auto i : range(2, N)) {
    int sieve = 1;
    for (auto p : P) {
      if (p * p > i) {
        break;
      } else if (i % p == 0) {
        sieve = 0;
        break;
      }
    }
    if (sieve == 0) {
      continue;
    } else {
      P.push_back(i);
    }
  }
  return P;
}

int main(void) {
  int N;
  cin >> N;
  auto A = input<int>(N);

  auto g = A[0];
  for (auto a : A) {
    g = gcd(g, a);
  }
  if (g > 1) {
    cout << "not coprime" << endl;
    return 0;
  }

  auto P = primes<int>(max(A));
  for (auto p : P) {
    int n = 0;
    for (auto a : A) {
      if (a % p == 0) {
        n += 1;
        if (n > 1) {
          cout << "setwise coprime" << endl;
          return 0;
        }
      }
    }
  }
  cout << "pairwise coprime" << endl;

  return 0;
}