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

#include <bits/stdc++.h>
using namespace std;

#define lli long long int
#define REP(i, s, n) for (int i = s; i < n; i++)
#define NUM 2520
#define INF (1LL << 50)
#define DEBUG 0
#define mp(a, b) make_pair(a, b)
#define SORT(V) sort(V.begin(), V.end())
#define PI (3.141592653589794)
#define TO_STRING(VariableName) #VariableName
#define LOG(x)                                                                 \\
  if (DEBUG)                                                                   \\
    cout << TO_STRING(x) << "=" << x << " " << endl;
#define LOG2(x, y)                                                             \\
  if (DEBUG)                                                                   \\
    cout << TO_STRING(x) << "=" << x << " " << TO_STRING(y) << "=" << y << endl;
#define LOG3(x, y, z)                                                          \\
  if (DEBUG)                                                                   \\
    cout << TO_STRING(x) << "=" << x << " " << TO_STRING(y) << "=" << y << " " \\
         << TO_STRING(z) << "=" << z << endl;
#define LOG4(w, x, y, z)                                                       \\
  if (DEBUG)                                                                   \\
    cout << TO_STRING(w) << "=" << w << " " << TO_STRING(x) << "=" << x << " " \\
         << TO_STRING(y) << "=" << y << " " << TO_STRING(z) << "=" << z        \\
         << endl;

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

lli GCD(lli x, lli y) {
  if (x < y)
    swap(x, y);
  if (y == 0) {
    return x;
  } else {
    return GCD(y, x % y);
  }
}
lli LCM(lli a, lli b) { return a / GCD(a, b) * b; }

lli d[1000100];
bool vis[1000100];

void func(long long N, std::vector<long long> A) {

  lli now = A[0];
  REP(i, 1, N) { now = GCD(now, A[i]); }
  if (now != 1) {
    cout << "not coprime" << endl;
    return;
  }

  set<lli> s;
  lli oneCnt = 0;
  REP(i, 0, N) {
    if (A[i] == 1)
      oneCnt++;
    else {
      s.insert(A[i]);
      d[A[i]] = 1;
    }
  }
  if (s.size() != N - oneCnt) {
    cout << "setwise coprime" << endl;
    return;
  }

  bool ok = true;
  for (lli i = 2; i < 1000100; i++) {
    if (vis[i])
      continue;
    if (!ok)
      break;
    lli now = i;
    lli cnt = 0;
    while (now < 1000100) {
      vis[now] = true;
      if (d[now])
        cnt++;
      if (cnt >= 2) {
        ok = false;
        break;
      }
      now += i;
    }
  }

  if (ok) {
    cout << "pairwise coprime" << endl;
  } else {
    cout << "setwise coprime" << endl;
  }
}

int main() {
  // cout << fixed << setprecision(5);

  long long N;
  scanf("%lld", &N);
  std::vector<long long> A(N);
  for (int i = 0; i < N; i++) {
    scanf("%lld", &A[i]);
  }
  func(N, std::move(A));
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define lli long long int
#define REP(i, s, n) for (lli i = s; i < n; i++)
#define NUM 2520
#define INF (1LL << 50)
#define DEBUG 0
#define mp(a, b) make_pair(a, b)
#define SORT(V) sort(V.begin(), V.end())
#define PI (3.141592653589794)
#define MOD (1000000007)
#define TO_STRING(VariableName) #VariableName
#define LOG(xx)                                                                \\
  if (DEBUG)                                                                   \\
    cout << TO_STRING(xx) << "=" << xx << " " << endl;
#define LOG2(xx, yy)                                                           \\
  if (DEBUG)                                                                   \\
    cout << TO_STRING(xx) << "=" << xx << " " << TO_STRING(yy) << "=" << yy    \\
         << endl;
#define LOG3(xx, yy, z)                                                        \\
  if (DEBUG)                                                                   \\
    cout << TO_STRING(xx) << "=" << xx << " " << TO_STRING(yy) << "=" << yy    \\
         << " " << TO_STRING(z) << "=" << z << endl;
#define LOG4(w, xx, yy, z)                                                     \\
  if (DEBUG)                                                                   \\
    cout << TO_STRING(w) << "=" << w << " " << TO_STRING(xx) << "=" << xx      \\
         << " " << TO_STRING(yy) << "=" << yy << " " << TO_STRING(z) << "="    \\
         << z << endl;

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
lli GCD(lli x, lli y) {
  if (x < y)
    swap(x, y);
  if (y == 0) {
    return x;
  } else {
    return GCD(y, x % y);
  }
}
lli ddata[1000010];
bool dd[1000010];
bool prime[1000010];
void solve() {

  lli n;
  cin >> n;
  vector<lli> a(n);
  set<lli> s;
  REP(i, 0, n) {
    cin >> a[i];
    ddata[a[i]]++;
    dd[a[i]] = true;
    s.insert(a[i]);
  }
  if (s.size() != n) {
    cout << "not coprime" << endl;
    return;
  }
  REP(i, 0, 1000010) prime[i] = true;

  bool ok = true;
  for (lli i = 2; i <= 1000010; i++) {
    if (!prime[i])
      continue;
    lli now = i;
    lli cnt = 1;
    lli cc = 0;
    while (now < 1000010) {
      if (dd[now]) {
        ddata[now]--;
        cc++;
      }
      cnt++;
      now = i * cnt;
      if (now >= 1000010)
        break;
      prime[now] = false;
    }
    if (cc >= 2) {
      ok = false;
    }
  }

  if (ok) {
    cout << "pairwise coprime" << endl;
    return;
  }
  lli nowG = a[0];
  REP(i, 1, n) { nowG = GCD(nowG, a[i]); }
  if (nowG == 1) {
    cout << "setwise coprime" << endl;
  } else {
    cout << "not coprime" << endl;
  }
}

int main() {

  // cout << fixxed << setprecision(5);
  lli t = 1;
  // cin>>t;
  while (t--)
    solve();

  return 0;
}