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

#include <bits/stdc++.h>
using namespace std;
typedef long long lint;
typedef vector<lint> vlint;
typedef vector<vlint> vvlint;
typedef vector<bool> vbool;
typedef vector<int> vint;

#define inf 1e+9
#define endl "\\n"
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i < (int)(n); i++)
#define rep_rev(i, n) for (int i = n - 1; i >= 0; --i)
#define all(x) (x).begin(), (x).end()

#define pb push_back
#define eb emplace_back
#define Size(c) (int)(c).size()

#define lb(c, x) distance((c).begin(), lower_bound(all(c), (x)))
#define ub(c, x) distance((c).begin(), upper_bound(all(c), (x)))

template <class T> using pq = priority_queue<T>;
template <class T> using pqg = priority_queue<T, vector<T>, greater<T>>;

template <class T, class S> inline bool chmax(T &a, S b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T, class S> inline bool chmin(T &a, S b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> void line(vector<T> &x) {
  for (auto &&xi : x)
    cin >> xi;
}

inline void IN(void) { return; }

template <typename First, typename... Rest>
void IN(First &first, Rest &...rest) {
  cin >> first;
  IN(rest...);
  return;
}

#define INT(...)                                                               \\
  int __VA_ARGS__;                                                             \\
  IN(__VA_ARGS__)
#define LINT(...)                                                              \\
  lint __VA_ARGS__;                                                            \\
  IN(__VA_ARGS__)
#define STR(...)                                                               \\
  string __VA_ARGS__;                                                          \\
  IN(__VA_ARGS__)

template <class T> void UNIQUE(vector<T> &x) {
  sort(all(x));
  x.erase(unique(all(x)), x.end());
}
template <class T> void print_vec(T first, T end) {
  for (auto i = first; i < end - 1; i++)
    cout << *i << " ";
  cout << *(end - 1) << endl;
}

template <class... T> void debug_print(T... args) {
  vector<lint> tmp = initializer_list<lint>{args...};
  print_vec(all(tmp));
}
template <class T> void print(T a) { cout << a << endl; }

vector<string> Yes = {"No", "Yes"};
vector<string> YES = {"NO", "YES"};

int ord(char x) { return (int)(x - 'a'); }
char chr(lint x) { return (char)(x + (lint)('a')); }
lint mod = 1e9 + 7;
// lint mod =

lint sum(vlint a) {
  lint ret = 0;
  for (auto &&v : a)
    ret += v;
  return ret;
}
lint vmini(vlint a, lint &index) {
  lint ret = LLONG_MAX;
  rep(i, Size(a)) {
    if (chmin(ret, a[i]))
      index = i;
  }
  return ret;
}
lint vmaxi(vlint a, lint &index) {
  lint ret = -LLONG_MAX;
  rep(i, Size(a)) {
    if (chmax(ret, a[i]))
      index = i;
  }
  return ret;
}

lint vmin(vlint a) {
  lint ret = LLONG_MAX;
  for (auto &&v : a)
    chmin(ret, v);
  return ret;
}
lint vmax(vlint a) {
  lint ret = -LLONG_MAX;
  for (auto &&v : a)
    chmax(ret, v);
  return ret;
}

vlint base_3(int k) {
  vlint ret;
  while (k > 0) {
    ret.pb(k % 3);
    k /= 3;
  }
  reverse(all(ret));
  return ret;
}
map<lint, lint> trial_division(lint n) {
  map<lint, lint> factor;
  lint tmp = (lint)(sqrt(n)) + 1;
  for (lint i = 2; i < tmp; ++i) {
    while (n % i == 0) {
      n /= i;
      factor[i]++;
    }
  }
  if (n != 1)
    factor[n]++;
  return factor;
}

lint dist(lint x1, lint y1, lint x2, lint y2) {
  return (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);
}

struct UnionFind {
  vector<int> par;
  int n;

  UnionFind(int N) : par(N + 1), n(N) { fill(all(par), -1); }

  int root(int x) {
    if (par[x] < 0)
      return x;
    return par[x] = root(par[x]);
  }

  void unite(int x, int y) {
    int rx = root(x);
    int ry = root(y);
    if (rx == ry)
      return;
    if (par[rx] > par[ry])
      swap(rx, ry);
    par[rx] += par[ry];
    par[ry] = rx;
  }

  bool same(int x, int y) {
    int rx = root(x);
    int ry = root(y);
    return rx == ry;
  }

  int size(int x) { return -par[root(x)]; }
};

vbool eratostheness(lint n) {

  vbool res(n, true);
  res[0] = res[1] = 0;

  lint i = 2;
  while (i * i <= n) {
    for (lint j = 2 * i; j < n + 1; j += i) {
      if (!(res[j]))
        continue;

      if (j % i == 0)
        res[j] = false;
    }
    i++;
  }
  return res;
}

int main() {
  INT(n);
  vlint a(n);
  line(a);
  int ans = 0;
  // lint x = vmax(a);
  // vbool ret = eratostheness(x);
  // vint search;
  // rep(i, x){
  // if (ret[i]) search.pb(i);
  //}
  // print_vec(all(search));
  bool flg1 = true;
  int tmp = 1;
  map<int, int> cnt;
  rep(i, n) {
    map<lint, lint> t = trial_division(a[i]);
    for (auto &&[k, v] : t) {
      if (k == 1)
        continue;
      cnt[k]++;
      if (cnt[k] >= 2) {
        tmp = k;
        flg1 = false;
        break;
      }
    }
    if (!flg1)
      break;
  }

  if (tmp != 1) {
    // print(tmp);
    bool flg;
    map<lint, lint> t0 = trial_division(a[0]);
    for (auto &&[pr, v] : t0) {
      if (a[1] % pr != 0)
        continue;
      flg = true;
      rep(i, n) {
        if (a[i] % pr != 0) {
          flg = false;
          break;
        }
      }
      if (flg)
        break;
    }

    if (flg)
      ans = 2;
    else
      ans = 1;
  }
  if (flg1)
    ans = 0;
  vector<string> answer = {"pairwise coprime", "setwise coprime",
                           "not coprime"};
  print(answer[ans]);
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long lint;
typedef vector<lint> vlint;
typedef vector<vlint> vvlint;
typedef vector<bool> vbool;
typedef vector<int> vint;

#define inf 1e+9
#define endl "\\n"
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i < (int)(n); i++)
#define rep_rev(i, n) for (int i = n - 1; i >= 0; --i)
#define all(x) (x).begin(), (x).end()

#define pb push_back
#define eb emplace_back
#define Size(c) (int)(c).size()

#define lb(c, x) distance((c).begin(), lower_bound(all(c), (x)))
#define ub(c, x) distance((c).begin(), upper_bound(all(c), (x)))

template <class T> using pq = priority_queue<T>;
template <class T> using pqg = priority_queue<T, vector<T>, greater<T>>;

template <class T, class S> inline bool chmax(T &a, S b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T, class S> inline bool chmin(T &a, S b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> void line(vector<T> &x) {
  for (auto &&xi : x)
    cin >> xi;
}

inline void IN(void) { return; }

template <typename First, typename... Rest>
void IN(First &first, Rest &...rest) {
  cin >> first;
  IN(rest...);
  return;
}

#define INT(...)                                                               \\
  int __VA_ARGS__;                                                             \\
  IN(__VA_ARGS__)
#define LINT(...)                                                              \\
  lint __VA_ARGS__;                                                            \\
  IN(__VA_ARGS__)
#define STR(...)                                                               \\
  string __VA_ARGS__;                                                          \\
  IN(__VA_ARGS__)

template <class T> void UNIQUE(vector<T> &x) {
  sort(all(x));
  x.erase(unique(all(x)), x.end());
}
template <class T> void print_vec(T first, T end) {
  for (auto i = first; i < end - 1; i++)
    cout << *i << " ";
  cout << *(end - 1) << endl;
}

template <class... T> void debug_print(T... args) {
  vector<lint> tmp = initializer_list<lint>{args...};
  print_vec(all(tmp));
}
template <class T> void print(T a) { cout << a << endl; }

vector<string> Yes = {"No", "Yes"};
vector<string> YES = {"NO", "YES"};

int ord(char x) { return (int)(x - 'a'); }
char chr(lint x) { return (char)(x + (lint)('a')); }
lint mod = 1e9 + 7;
// lint mod =

lint sum(vlint a) {
  lint ret = 0;
  for (auto &&v : a)
    ret += v;
  return ret;
}
lint vmini(vlint a, lint &index) {
  lint ret = LLONG_MAX;
  rep(i, Size(a)) {
    if (chmin(ret, a[i]))
      index = i;
  }
  return ret;
}
lint vmaxi(vlint a, lint &index) {
  lint ret = -LLONG_MAX;
  rep(i, Size(a)) {
    if (chmax(ret, a[i]))
      index = i;
  }
  return ret;
}

lint vmin(vlint a) {
  lint ret = LLONG_MAX;
  for (auto &&v : a)
    chmin(ret, v);
  return ret;
}
lint vmax(vlint a) {
  lint ret = -LLONG_MAX;
  for (auto &&v : a)
    chmax(ret, v);
  return ret;
}

vlint base_3(int k) {
  vlint ret;
  while (k > 0) {
    ret.pb(k % 3);
    k /= 3;
  }
  reverse(all(ret));
  return ret;
}
map<lint, lint> trial_division(lint n) {
  map<lint, lint> factor;
  lint tmp = (lint)(sqrt(n)) + 1;
  for (lint i = 2; i < tmp; ++i) {
    while (n % i == 0) {
      n /= i;
      factor[i]++;
    }
  }
  if (n != 1)
    factor[n]++;
  return factor;
}

lint dist(lint x1, lint y1, lint x2, lint y2) {
  return (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);
}

struct UnionFind {
  vector<int> par;
  int n;

  UnionFind(int N) : par(N + 1), n(N) { fill(all(par), -1); }

  int root(int x) {
    if (par[x] < 0)
      return x;
    return par[x] = root(par[x]);
  }

  void unite(int x, int y) {
    int rx = root(x);
    int ry = root(y);
    if (rx == ry)
      return;
    if (par[rx] > par[ry])
      swap(rx, ry);
    par[rx] += par[ry];
    par[ry] = rx;
  }

  bool same(int x, int y) {
    int rx = root(x);
    int ry = root(y);
    return rx == ry;
  }

  int size(int x) { return -par[root(x)]; }
};

vlint eratostheness(lint n) {

  vlint res(n, 1);
  res[0] = res[1] = 0;

  lint i = 2;
  while (i * i <= n) {
    for (lint j = 2 * i; j < n + 1; j += i) {
      if (!(res[j]))
        continue;

      if (j % i == 0)
        res[j] = 0;
    }
    i++;
  }
  return res;
}

int main() {
  INT(n);
  vlint a(n);
  line(a);
  int ans = -1;
  lint x = vmax(a);
  vlint ret = eratostheness(x);
  vlint search;
  rep(i, x) {
    if (ret[i])
      search.pb(i);
  }
  // print_vec(all(search));
  bool flg1 = true;

  int cnt;
  lint tmp = 1;
  for (auto &&pr : search) {
    cnt = 0;
    rep(i, n) {
      // print(pr);
      if (a[i] % pr == 0) {
        cnt++;
        if (cnt >= 2) {
          flg1 = false;
          tmp = pr;
          break;
        }
      }
    }
    if (!flg1)
      break;
  }
  if (tmp != 1) {
    // print(tmp);
    bool flg = true;
    rep(i, n) {
      if (a[i] % tmp != 0) {
        flg = false;
        break;
      }
    }
    if (flg)
      ans = 2;
    else
      ans = 1;
  }
  if (flg1)
    ans = 0;
  vector<string> answer = {"pairwise coprime", "setwise coprime",
                           "not coprime"};
  print(answer[ans]);
}