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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep1(i, n) for (int i = 1; i < n; i++)
#define all(v) v.begin(), v.end()
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
typedef int64_t lint;
lint gcd(lint a, lint b) { return b ? gcd(b, a % b) : a; }
lint lcm(lint a, lint b) { return a / gcd(a, b) * b; }

int main() {
  int N;
  cin >> N;
  vector<int> v(N);
  rep(i, N) { cin >> v.at(i); }

  int a = 1000001, b = 0, c = v.at(0), x, y, z;
  vector<bool> P(a);
  fill(all(P), true);
  P.at(0) = false, P.at(1) = false;
  for (int i = 2; i * i <= a - 1; ++i) {
    if (P.at(i)) {
      for (int j = 2; i * j <= a - 1; ++j) {
        P.at(i * j) = false;
      }
    }
  }

  vector<int> PV;
  rep(i, a) {
    if (P.at(i))
      PV.push_back(i);
  }

  bool f = true;
  rep(i, N) {
    x = v.at(i);
    rep(j, PV.size()) {
      y = PV.at(j);
      if (P.at(x)) {
        P.at(x) = false;
        break;
      }
      if (x < y)
        break;
      if (x % y == 0) {
        if (!P.at(y)) {
          f = false;
          break;
        }
        while (x % y == 0)
          x /= y;
        P.at(y) = false;
      }
    }
    if (!f)
      break;
  }
  if (f) {
    std::cout << "pairwise coprime" << '\\n';
    return 0;
  }

  rep1(i, N) {
    c = gcd(c, v.at(i));
    if (c == 1) {
      std::cout << "setwise coprime" << '\\n';
      return 0;
    }
  }

  std::cout << "not coprime" << '\\n';
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep1(i, n) for (int i = 1; i < n; i++)
#define all(v) v.begin(), v.end()
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
typedef int64_t lint;
lint gcd(int a, int b) { return b ? gcd(b, a % b) : a; }

int main() {
  int N;
  cin >> N;
  vector<int> v(N);
  rep(i, N) { cin >> v.at(i); }

  int a = 1000001, b = 0, c = 0, x, y, z;
  vector<bool> P(a);
  fill(all(P), true);
  P.at(0) = false, P.at(1) = false;
  for (int i = 2; i * i <= a - 1; ++i) {
    if (P.at(i)) {
      for (int j = 2; i * j <= a - 1; ++j) {
        P.at(i * j) = false;
      }
    }
  }

  vector<int> PV;
  rep(i, a) {
    if (P.at(i))
      PV.push_back(i);
  }

  bool f = true;
  rep(i, N) {
    x = v.at(i);
    rep(j, PV.size()) {
      y = PV.at(j);
      if (x < y)
        break;
      if (x % y == 0) {
        if (!P.at(y)) {
          f = false;
          break;
        }
        while (x % y == 0)
          x /= y;
        P.at(y) = false;
      }
    }
    if (!f)
      break;
  }
  if (f) {
    std::cout << "pairwise coprime" << '\\n';
    return 0;
  }

  c = v.at(0);
  rep1(i, N) {
    c = gcd(c, v.at(i));
    if (c == 1) {
      std::cout << "setwise coprime" << '\\n';
      return 0;
    }
  }

  std::cout << "not coprime" << '\\n';
}