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

#include <bits/stdc++.h>
#define F(i, a, b) for (ll i = a; i <= (b); ++i)
#define dF(i, a, b) for (int i = a; i >= (b); --i)
#define inf 0x3f3f3f3f
#define infll 0x3f3f3f3f3f3f3f3f
#define pb push_back
#define maxnkp make_pair
#define fi first
#define se second
#define eps 1e-7
#define PI acos(-1.0)
using namespace std;
typedef long long ll;
const int mods = 1e9 + 7;
const int maxn = 1e6 + 10;
const int N = 1e5 + 10;
const int E = 1e6 + 10;

int n;
ll a[maxn];
ll fac;
ll prime[maxn]; // 下标从1开始
bool not_prime[maxn];

int sieve(int n) { // 把2到n内所有素数筛选出来
  int p = 0;
  for (int i = 2; i <= n; ++i) {
    if (!not_prime[i])
      prime[++p] = i;
    for (int j = 1; j <= p && i * prime[j] <= n; j++) {
      not_prime[i * prime[j]] = true; // 把比最小素因子的素数的倍数筛除
      if (i % prime[j] == 0)
        break; // 已找到最小素因子
    }
  }
  return p; //  返回素数个数
}
int cnt[maxn];
bool flg = 1;
void pri_fac(int x) {
  for (int i = 1; prime[i] * prime[i] <= x; i++) {
    if (x % prime[i] == 0) {
      if (cnt[prime[i]]) {
        flg = 0;
        break;
      } else
        cnt[prime[i]] = 1;
      while (x % prime[i] == 0)
        x /= prime[i];
    }
  }
  if (x > 1) {
    if (cnt[x]) {
      flg = 0;
      return;
    } else
      cnt[x] = 1;
  }
}

int main() {
  int len = sieve(1000000);
  cin >> n;
  F(i, 1, n) { cin >> a[i]; }
  ll gg = a[1];
  F(i, 2, n) { gg = __gcd(gg, a[i]); }
  if (gg != 1) {
    printf("not coprime\\n");
    return 0;
  }
  F(i, 1, n) {
    pri_fac(a[i]);
    if (!flg)
      break;
  }
  if (flg) {
    printf("pairwise coprime\\n");
  } else {
    printf("setwise coprime\\n");
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define F(i, a, b) for (ll i = a; i <= (b); ++i)
#define dF(i, a, b) for (int i = a; i >= (b); --i)
#define inf 0x3f3f3f3f
#define infll 0x3f3f3f3f3f3f3f3f
#define pb push_back
#define maxnkp make_pair
#define fi first
#define se second
#define eps 1e-7
#define PI acos(-1.0)
using namespace std;
typedef long long ll;
const int mods = 1e9 + 7;
const int maxn = 1e6 + 10;
const int N = 1e5 + 10;
const int E = 1e6 + 10;

int n;
ll a[maxn];
ll fac;
ll prime[maxn]; // 下标从1开始
bool not_prime[maxn];

int sieve(int n) { // 把2到n内所有素数筛选出来
  int p = 0;
  for (int i = 2; i <= n; ++i) {
    if (!not_prime[i])
      prime[++p] = i;
    for (int j = 1; j <= p && i * prime[j] <= n; j++) {
      not_prime[i * prime[j]] = true; // 把比最小素因子的素数的倍数筛除
      if (i % prime[j] == 0)
        break; // 已找到最小素因子
    }
  }
  return p; //  返回素数个数
}
int cnt[maxn];
bool flg = 1;
void pri_fac(int x) {
  for (int i = 1; prime[i] * prime[i] <= x; i++) {
    if (x % prime[i] == 0) {
      if (cnt[prime[i]]) {
        flg = 0;
        break;
      } else
        cnt[prime[i]] = 1;
      while (x % prime[i] == 0)
        x /= prime[i];
    }
  }
  if (x > 1) {
    if (cnt[x]) {
      flg = 0;
      return;
    } else
      cnt[x] = 1;
  }
}

int main() {
  int len = sieve(1000);
  cin >> n;
  F(i, 1, n) { cin >> a[i]; }
  ll gg = a[1];
  F(i, 2, n) { gg = __gcd(gg, a[i]); }
  if (gg != 1) {
    printf("not coprime\\n");
    return 0;
  }
  F(i, 1, n) {
    pri_fac(a[i]);
    if (!flg)
      break;
  }
  if (flg) {
    printf("pairwise coprime\\n");
  } else {
    printf("setwise coprime\\n");
  }
}