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

#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std;

#define LL long long

const int MAXN = 10000000 + 5;
const LL MOD = 1000000000 + 7;

int c[MAXN], a[MAXN];

void divide(int m) {

  for (int i = 2; i * i <= m; i++) {
    while (m % i == 0) {
      c[i]++;
      m /= i;
    }
  }
  if (m > 1) {
    c[m]++;
  }
}

bool isPairwiseCoprime(int m) {
  bool pairwiseCoprime = true;
  vector<pair<int, int>> f;
  f.clear();
  for (int i = 2; i * i <= m; i++) {
    int cnt = 0;
    if (m % i == 0) {
      while (m % i == 0) {
        c[i]--;
        m /= i;
        cnt++;
      }
      f.push_back(make_pair(i, cnt));
    }
  }
  if (m > 1) {
    c[m]--;
    f.push_back(make_pair(m, 1));
  }
  for (int i = 0; i < f.size(); i++) {
    if (c[f[i].first] > 0) {
      pairwiseCoprime = false;
      break;
    }
  }
  for (int i = 0; i < f.size(); i++) {
    c[f[i].first] += f[i].second;
  }
  return pairwiseCoprime;
}

int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }

int main() {
  int N, maxa = 0;
  memset(c, 0, sizeof(c));
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> a[i];
    maxa = max(a[i], maxa);
    divide(a[i]);
  }
  bool pairwiseCoprime = true;
  for (int i = 0; i < N; i++) {
    if (isPairwiseCoprime(a[i]) == false) {
      pairwiseCoprime = false;
      break;
    }
  }
  if (pairwiseCoprime) {
    cout << "pairwise coprime";
  } else {
    int factor = gcd(a[0], a[1]);
    for (int i = 2; i < N; i++) {
      factor = gcd(factor, a[i]);
      if (factor <= 1) {
        break;
      }
    }
    if (factor <= 1) {
      cout << "setwise  coprime";
    } else {
      cout << "not coprime";
    }
  }
  system("pause");
  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std;

#define LL long long

const int MAXN = 1000000 + 5;
const LL MOD = 1000000000 + 7;

int c[MAXN], a[MAXN];

int divide(int m) {

  for (int i = 2; i * i <= m; i++) {
    while (m % i == 0) {
      c[i]++;
      m /= i;
    }
  }
  if (m > 1) {
    c[m]++;
  }
}

bool isPairwiseCoprime(int m) {
  bool pairwiseCoprime = true;
  vector<pair<int, int>> f;
  f.clear();
  for (int i = 2; i * i <= m; i++) {
    int cnt = 0;
    if (m % i == 0) {
      while (m % i == 0) {
        c[i]--;
        m /= i;
        cnt++;
      }
      f.push_back(make_pair(i, cnt));
    }
  }
  for (int i = 0; i < f.size(); i++) {
    if (c[f[0].first] > 0) {
      pairwiseCoprime = false;
      break;
    }
  }
  for (int i = 0; i < f.size(); i++) {
    c[f[i].first] += f[i].second;
  }
  return pairwiseCoprime;
}

int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }

int main() {
  int N, maxa = 0;
  memset(c, 0, sizeof(c));
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> a[i];
    maxa = max(a[i], maxa);
    divide(a[i]);
  }
  bool pairwiseCoprime = true;
  for (int i = 0; i < N; i++) {
    if (isPairwiseCoprime(a[i]) == false) {
      pairwiseCoprime = false;
      break;
    }
  }
  if (pairwiseCoprime) {
    cout << "pairwise coprime";
  } else {
    int factor = gcd(a[0], a[1]);
    for (int i = 2; i < N; i++) {
      factor = gcd(factor, a[i]);
      if (factor <= 1) {
        break;
      }
    }
    if (factor <= 1) {
      cout << "setwise  coprime";
    } else {
      cout << "not coprime";
    }
  }
  system("pause");
  return 0;
}