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

#include <algorithm>
#include <array>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

template <class... Args> void Input(Args &...args) { (cin >> ... >> args); }

template <class T> void InputArray(std::size_t size, T &t) {
  t.resize(size);
  for (auto &e : t) {
    cin >> e;
  }
}

template <class... Args> void InputArray(std::size_t size, Args &...args) {
  (InputArray(size, args), ...);
}

template <class... Args>
void InputVerticalArray(std::size_t size, Args &...args) {
  (args.resize(size), ...);
  for (int i = 0; i < size; i++) {
    ((cin >> args[i]), ...);
  }
}

template <class... Args> void Output(const Args &...args) {
  ((cout << args << ' '), ...) << endl;
}

template <class... Args> void OutputVertical(const Args &...args) {
  ((cout << args << endl), ...);
}

template <class T> void OutputArray(const T &t) {
  for (const auto &e : t) {
    cout << e << ' ';
  }
  cout << endl;
}

template <class... Args> void OutputArray(const Args &...args) {
  (OutputArray(args), ...);
}

template <class... Args> void OutputVerticalArray(const Args &...args) {
  for (int i = 0; i < min({args.size()...}); i++) {
    ((cout << args[i] << ' '), ...) << endl;
  }
}

#define inp(type, ...)                                                         \\
  type __VA_ARGS__;                                                            \\
  Input(__VA_ARGS__)

#define inpa(type, size, ...)                                                  \\
  type __VA_ARGS__;                                                            \\
  InputArray((size), __VA_ARGS__)

#define inpva(type, size, ...)                                                 \\
  type __VA_ARGS__;                                                            \\
  InputVerticalArray((size), __VA_ARGS__)

#define otp(...) Output(__VA_ARGS__)

#define otpv(...) OutputVertical(__VA_ARGS__)

#define otpa(...) OutputArray(__VA_ARGS__)

#define otpva(...) OutputVerticalArray(__VA_ARGS__)

#define rep(counter, range)                                                    \\
  for (int(counter) = 0; (counter) < (range); (counter)++)

#define repi(range) rep(i, range)

#define rrep(counter, range)                                                   \\
  for (int(counter) = (range)-1; (counter) >= 0; (counter)--)

#define rrepi(range) rrep(i, range)

#define repc(element, container) for (auto &(element) : (container))

#define repe(container) repc(e, container)

#define all(container) (container).begin(), (container).end()

#ifdef __CCR__
#define dlog(x) clog << #x " = " << x << endl
#else
#define dlog(x)
#endif

vector<int> factor(1000001);

void InitFactor() {
  for (int i = 2; i <= 1000000; i++) {
    if (factor[i] == 0)
      factor[i] = i;
    for (int j = i + i; j <= 1000000; j += i) {
      factor[j] = i;
    }
  }
}

int main() {
  inp(int, n);
  inpa(vector<int>, n, a);

  InitFactor();
  map<int, int> cnt;
  int fc;
  fc = a[0];
  for (int i = 1; i < n; i++) {
    fc = gcd(fc, a[i]);
  }
  bool setwise = fc == 1;
  repe(a) {
    while (e > 1) {
      fc = factor[e];
      do {
        e /= fc;
      } while (e % fc == 0);
      cnt[fc]++;
      if (cnt[fc] > 1) {
        otp(setwise ? "setwise coprime" : "not coprime");
        return 0;
      }
    }
  }
  otp("pairwise coprime");
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <array>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

template <class... Args> void Input(Args &...args) { (cin >> ... >> args); }

template <class T> void InputArray(std::size_t size, T &t) {
  t.resize(size);
  for (auto &e : t) {
    cin >> e;
  }
}

template <class... Args> void InputArray(std::size_t size, Args &...args) {
  (InputArray(size, args), ...);
}

template <class... Args>
void InputVerticalArray(std::size_t size, Args &...args) {
  (args.resize(size), ...);
  for (int i = 0; i < size; i++) {
    ((cin >> args[i]), ...);
  }
}

template <class... Args> void Output(const Args &...args) {
  ((cout << args << ' '), ...) << endl;
}

template <class... Args> void OutputVertical(const Args &...args) {
  ((cout << args << endl), ...);
}

template <class T> void OutputArray(const T &t) {
  for (const auto &e : t) {
    cout << e << ' ';
  }
  cout << endl;
}

template <class... Args> void OutputArray(const Args &...args) {
  (OutputArray(args), ...);
}

template <class... Args> void OutputVerticalArray(const Args &...args) {
  for (int i = 0; i < min({args.size()...}); i++) {
    ((cout << args[i] << ' '), ...) << endl;
  }
}

#define inp(type, ...)                                                         \\
  type __VA_ARGS__;                                                            \\
  Input(__VA_ARGS__)

#define inpa(type, size, ...)                                                  \\
  type __VA_ARGS__;                                                            \\
  InputArray((size), __VA_ARGS__)

#define inpva(type, size, ...)                                                 \\
  type __VA_ARGS__;                                                            \\
  InputVerticalArray((size), __VA_ARGS__)

#define otp(...) Output(__VA_ARGS__)

#define otpv(...) OutputVertical(__VA_ARGS__)

#define otpa(...) OutputArray(__VA_ARGS__)

#define otpva(...) OutputVerticalArray(__VA_ARGS__)

#define rep(counter, range)                                                    \\
  for (int(counter) = 0; (counter) < (range); (counter)++)

#define repi(range) rep(i, range)

#define rrep(counter, range)                                                   \\
  for (int(counter) = (range)-1; (counter) >= 0; (counter)--)

#define rrepi(range) rrep(i, range)

#define repc(element, container) for (auto &(element) : (container))

#define repe(container) repc(e, container)

#define all(container) (container).begin(), (container).end()

#ifdef __CCR__
#define dlog(x) clog << #x " = " << x << endl
#else
#define dlog(x)
#endif

vector<int> factor(1000001);

void InitFactor() {
  for (int i = 0; i <= 1000000; i++) {
    if (factor[i] == 0)
      factor[i] = i;
    for (int j = i + i; j <= 1000000; j += i) {
      factor[j] = i;
    }
  }
}

int main() {
  inp(int, n);
  inpa(vector<int>, n, a);

  InitFactor();
  map<int, int> cnt;
  int fc;
  repe(a) {
    while (e > 1) {
      fc = factor[e];
      do {
        e /= fc;
      } while (e % fc == 0);
      cnt[fc]++;
    }
  }

  int cntmax = 1;
  for (auto [key, e] : cnt) {
    if (cntmax < e)
      cntmax = e;
  }
  if (cntmax == 1) {
    otp("pairwise coprime");
  } else if (cntmax < n) {
    otp("setwise coprime");
  } else {
    otp("not coprime");
  }

  return 0;
}