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

#include <iostream>
#include <numeric>

inline int read() {
  int x = 0, f = 1;
  char c = getchar();
  while (!isdigit(c)) {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (isdigit(c)) {
    x = x * 10 + c - '0';
    c = getchar();
  }
  return x * f;
}

int cnt[1000001];

int main() {
  int n = read();
  int num = read();
  int o = 0, g = num;
  if (num == 1)
    o = 1;
  int ex = (n - 1) / 8;
  cnt[num] = 1;

  for (int i = 0; i < ex; i++) {
    int num = read();
    if (num == 1)
      o++;
    g = std::gcd(g, num);
    cnt[num]++;

    num = read();
    if (num == 1)
      o++;
    g = std::gcd(g, num);
    cnt[num]++;

    num = read();
    if (num == 1)
      o++;
    g = std::gcd(g, num);
    cnt[num]++;

    num = read();
    if (num == 1)
      o++;
    g = std::gcd(g, num);
    cnt[num]++;

    num = read();
    if (num == 1)
      o++;
    g = std::gcd(g, num);
    cnt[num]++;

    num = read();
    if (num == 1)
      o++;
    g = std::gcd(g, num);
    cnt[num]++;

    num = read();
    if (num == 1)
      o++;
    g = std::gcd(g, num);
    cnt[num]++;

    num = read();
    if (num == 1)
      o++;
    g = std::gcd(g, num);
    cnt[num]++;
  }

  for (int i = 0; i < (n - 1) % 8; i++) {
    int num = read();
    if (num == 1)
      o++;
    g = std::gcd(g, num);
    cnt[num]++;
  }

  if (n - o > 80000) {
    if (g == 1)
      printf("setwise coprime\\n");
    else
      printf("not coprime\\n");
    return 0;
  }

  int use[1000001];
  int max_S = 0;
  for (int i = 2; i <= 1000000; i++) {
    if (use[i] == -1)
      continue;
    int S = 0;
    for (int j = i; j <= 1000000; j += i) {
      S += cnt[j];
      use[j] = -1;
    }
    if (max_S < S)
      max_S = S;
  }
  if (max_S < 2)
    printf("pairwise coprime\\n");
  else if (max_S != n)
    printf("setwise coprime\\n");
  else
    printf("not coprime\\n");
}


Error: -11

Fixed submission:

#include <iostream>
#include <numeric>

inline int gcd(int a, int b) {
  if (b < a)
    std::swap(a, b);
  int r = a % b;
  if (r == 0)
    return b;
  while (r != 0)
    r = a % b, a = b, b = r;
  return a;
}

inline int read() {
  int x = 0, f = 1;
  char c = getchar();
  while (!isdigit(c)) {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (isdigit(c)) {
    x = x * 10 + c - '0';
    c = getchar();
  }
  return x * f;
}

int cnt[1000001];
bool use[1000001];

int main() {
  int n = read();
  int num = read();
  int o = 0, g = num;
  if (num == 1)
    o = 1;
  int ex = (n - 1) / 8;
  cnt[num] = 1;

  for (int i = 0; i < ex; i++) {
    int num = read();
    if (num == 1)
      o++;
    g = gcd(g, num);
    cnt[num]++;

    num = read();
    if (num == 1)
      o++;
    g = gcd(g, num);
    cnt[num]++;

    num = read();
    if (num == 1)
      o++;
    g = gcd(g, num);
    cnt[num]++;

    num = read();
    if (num == 1)
      o++;
    g = gcd(g, num);
    cnt[num]++;

    num = read();
    if (num == 1)
      o++;
    g = gcd(g, num);
    cnt[num]++;

    num = read();
    if (num == 1)
      o++;
    g = gcd(g, num);
    cnt[num]++;

    num = read();
    if (num == 1)
      o++;
    g = gcd(g, num);
    cnt[num]++;

    num = read();
    if (num == 1)
      o++;
    g = gcd(g, num);
    cnt[num]++;
  }

  for (int i = 0; i < (n - 1) % 8; i++) {
    int num = read();
    if (num == 1)
      o++;
    g = gcd(g, num);
    cnt[num]++;
  }

  if (n - o > 80000) {
    if (g == 1)
      printf("setwise coprime\\n");
    else
      printf("not coprime\\n");
    return 0;
  }
  int max_S = 0;
  for (int i = 2; i <= 1000000; i++) {
    if (use[i])
      continue;
    int S = 0;
    for (int j = i * i; j <= 1000000; j += i)
      use[j] = 1;
    for (int j = i; j <= 1000000; j += i)
      S += cnt[j];
    if (max_S < S)
      max_S = S;
  }
  if (max_S < 2)
    printf("pairwise coprime\\n");
  else if (max_S != n)
    printf("setwise coprime\\n");
  else
    printf("not coprime\\n");
}