Problem p02574 (C++) — buggy submission, verdict: Runtime Error

#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()
typedef int64_t lint;
lint gcd(lint a, lint b) { return b ? gcd(b, a % b) : a; }
struct Sieve {
  int n;
  vector<int> p;
  Sieve(int n = 1) : n(n), p(n + 1) {
    p.at(0) = 0, p.at(1) = 0;
    for (lint i = 2; i <= n; i++) {
      if (!p.at(i)) {
        p.at(i) = i;
        for (lint j = i * i; j <= n; j += i) {
          if (!p.at(j))
            p.at(j) = i;
        }
      }
    }
  }
  bool isPrime(int x) { return p.at(x) == x; }
  vector<int> factorList(int x) {
    vector<int> v;
    while (x != 1) {
      v.push_back(p.at(x));
      x /= p.at(x);
    }
    return v;
  }
  vector<int> factor(int x) {
    vector<int> v;
    while (x != 1) {
      int t = p.at(x);
      v.push_back(t);
      while (x % t == 0)
        x /= t;
    }
    return v;
  }
};

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

  int a = 0, b = 0, c = v.at(0), x, y, z;
  Sieve S(1000000);
  set<int> P;
  rep(i, N) {
    x = v.at(i);
    for (int j : S.factor(x)) {
      if (P.count(j)) {
        rep1(i, N) {
          c = gcd(c, v.at(i));
          if (c == 1) {
            std::cout << "setwise coprime" << '\\n';
            return 0;
          }
        }
        std::cout << "not coprime" << '\\n';
        return 0;
      }
      P.insert(j);
    }
  }
  std::cout << "pairwise coprime" << '\\n';
}


Error: 0

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(lint a, lint b) { return b ? gcd(b, a % b) : a; }
template <class T> inline void vecout(vector<T> v) {
  rep(i, v.size()) { cout << v.at(i) << ' '; }
  cout << endl;
}
struct Sieve {
  int n;
  vector<int> p;
  Sieve(int n = 1) : n(n), p(n + 1) {
    p.at(0) = 0, p.at(1) = 0;
    for (lint i = 2; i * i <= n; i++) {
      if (!p.at(i)) {
        p.at(i) = i;
        for (lint j = i * i; j <= n; j += i) {
          if (!p.at(j))
            p.at(j) = i;
        }
      }
    }
  }
  bool isPrime(int x) { return p.at(x) == x; }
  vector<int> factorList(int x) {
    vector<int> v;
    while (x != 1) {
      v.push_back(p.at(x));
      x /= p.at(x);
    }
    return v;
  }
  vector<int> factor(int x) {
    vector<int> v;
    while (x != 1) {
      int t = p.at(x);
      v.push_back(t);
      while (x % t == 0)
        x /= t;
    }
    return v;
  }
};

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

  int a = 0, b = 0, c = v.at(0), x, y, z;
  Sieve S(1000000);
  set<int> P;
  bool f = true;
  rep(i, N) {
    x = v.at(i);
    for (int j : S.factor(x)) {
      if (P.count(j)) {
        f = false;
        break;
      }
      P.insert(j);
    }
    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';
}