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

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <ctype.h>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define REP(i, n) for (ll i = 0; i < (ll)(n); ++i)
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;
}

int D[1000005];
int prime[1000005];

int GCD(int a, int b) {
  if (b == 0)
    return a;

  return GCD(b, a % b);
}

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  int wholegcd = 0, maxA = 0;
  REP(i, n) {
    cin >> a[i];
    chmax(maxA, a[i]);
    if (i == 0)
      wholegcd = a[i];
    else
      wholegcd = GCD(wholegcd, a[i]);
  }

  for (int i = 0; i <= maxA; i++) {
    D[i] = 0;
    prime[i] = 0;
  }

  D[1] = 1;
  for (int i = 2; i <= maxA; i++) {
    if (D[i] != 0)
      continue;
    D[i] = i;
    for (int j = i; (ll)i * j <= maxA; j++) {
      if (D[i * j] == 0)
        D[i * j] = i;
    }
  }

  bool pairwise = true;
  REP(i, n) {
    if (a[i] == 1)
      continue;
    int now = a[i];
    bool exitloop = false;

    set<int> p;
    while (D[now] != now) {
      p.insert(D[now]);
      now /= D[now];
    }
    if (now != 1)
      p.insert(now);

    for (int x : p) {
      if (prime[x] == 1) {
        exitloop = true;
        break;
      } else {
        prime[x] = 1;
      }
    }

    if (exitloop) {
      pairwise = false;
      break;
    }
  }

  if (pairwise) {
    cout << "pairwise coprime" << endl;
  } else if (wholegcd == 1) {
    cout << "setwise coprime" << endl;
  } else {
    cout << "not coprime" << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <ctype.h>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define REP(i, n) for (ll i = 0; i < (ll)(n); ++i)
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;
}

int D[1000005];
int prime[1000005];

int GCD(int a, int b) {
  if (b == 0)
    return a;

  return GCD(b, a % b);
}

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  int wholegcd = 0, maxA = 0;
  REP(i, n) {
    cin >> a[i];
    chmax(maxA, a[i]);
    if (i == 0)
      wholegcd = a[i];
    else
      wholegcd = GCD(wholegcd, a[i]);
  }

  D[1] = 1;
  for (int i = 2; i <= maxA; i++) {
    if (D[i] != 0)
      continue;
    D[i] = i;
    for (int j = i; i * j <= maxA; j++) {
      if (D[i * j] == 0)
        D[i * j] = i;
    }
  }

  bool pairwise = true;
  REP(i, n) {
    if (a[i] == 1)
      continue;
    int now = a[i];
    if (prime[now] == 1) {
      pairwise = false;
      break;
    }
    prime[now] = 1;
    while (D[now] != now) {
      if (prime[D[now]] == 1) {
        pairwise = false;
        break;
      }
      prime[D[now]] = 1;

      if (D[now] == now / D[now]) {
        now = D[now];
        continue;
      }
      now /= D[now];

      if (prime[now] == 1) {
        pairwise = false;
        break;
      }
      prime[now] = 1;
    }
  }

  if (pairwise) {
    cout << "pairwise coprime" << endl;
  } else if (wholegcd == 1) {
    cout << "setwise coprime" << endl;
  } else {
    cout << "not coprime" << endl;
  }

  return 0;
}