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

#include <bits/stdc++.h>
#define LL long long
#define ULL unsigned long long
#define UI unsigned int
#define mem(i, j) memset(i, j, sizeof(i))
#define rep(i, j, k) for (int i = j; i <= k; i++)
#define dep(i, j, k) for (int i = k; i >= j; i--)
#define pb push_back
#define make make_pair
#define INF 0x3f3f3f3f
#define inf LLONG_MAX
#define PI acos(-1)
#define fir first
#define sec second
#define lb(x) ((x) & (-(x)))
#define dbg(x) cout << #x << " = " << x << endl;
using namespace std;

const int N = 1e6 + 5;

const LL mod = 1e9 + 7;

int n, a[N];

bool vis[N];

int pre[N], tot;

bool see[N];

void init(int up) {

  rep(i, 2, up) {

    if (!vis[i])
      pre[++tot] = i;

    rep(j, 1, tot) {

      if (i * pre[j] > up)
        break;

      vis[i * pre[j]] = 1;

      if (i % pre[j] == 0)
        break;
    }
  }

  return;
}

bool judge1() {

  rep(i, 1, n) {

    for (int j = 1; pre[j] * pre[j] <= a[i]; j++) {

      if (a[i] % pre[j] == 0) {

        if (see[pre[j]])
          return 0;

        see[pre[j]] = 1;

        while (a[i] % pre[j] == 0) {

          a[i] /= pre[j];
        }
      }
    }

    if (a[i] != 1) {

      if (see[a[i]])
        return 0;

      see[a[i]] = 1;
    }
  }

  return 1;
}

void solve() {

  scanf("%d", &n);

  int ma = 0;

  rep(i, 1, n) scanf("%d", &a[i]), ma = max(ma, a[i]);

  if (ma == 1) {

    puts("pairwise coprime");

    return;
  }

  init(ma);

  int G = a[1];

  rep(i, 2, n) G = __gcd(G, a[i]);

  if (judge1())
    puts("pairwise coprime");

  else if (G == 1)
    puts("setwise coprime");

  else
    puts("not coprime");

  return;
}

int main() {

  //    int _; scanf("%d", &_);
  //    while(_--) solve();

  solve();

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define LL long long
#define ULL unsigned long long
#define UI unsigned int
#define mem(i, j) memset(i, j, sizeof(i))
#define rep(i, j, k) for (int i = j; i <= k; i++)
#define dep(i, j, k) for (int i = k; i >= j; i--)
#define pb push_back
#define make make_pair
#define INF 0x3f3f3f3f
#define inf LLONG_MAX
#define PI acos(-1)
#define fir first
#define sec second
#define lb(x) ((x) & (-(x)))
#define dbg(x) cout << #x << " = " << x << endl;
using namespace std;

const int N = 2e6 + 5;

const LL mod = 1e9 + 7;

int n, a[N];

bool vis[N];

int pre[N], tot;

bool see[N];

void init(int up) {

  rep(i, 2, up) {

    if (!vis[i])
      pre[++tot] = i;

    rep(j, 1, tot) {

      if (i * pre[j] > up)
        break;

      vis[i * pre[j]] = 1;

      if (i % pre[j] == 0)
        break;
    }
  }
}

bool judge1() {

  rep(i, 1, n) {

    for (int j = 1; pre[j] * pre[j] <= a[i]; j++) {

      if (a[i] % pre[j] == 0) {

        if (see[pre[j]])
          return 0;

        see[pre[j]] = 1;

        while (a[i] % pre[j] == 0) {

          a[i] /= pre[j];
        }
      }
    }

    if (a[i] != 1) {

      if (see[a[i]])
        return 0;

      see[a[i]] = 1;
    }
  }

  return 1;
}

void solve() {

  scanf("%d", &n);

  int ma = 0;

  rep(i, 1, n) scanf("%d", &a[i]), ma = max(ma, a[i]);

  init(ma);

  int G = a[1];

  rep(i, 2, n) G = __gcd(G, a[i]);

  if (judge1())
    puts("pairwise coprime");

  else if (G == 1)
    puts("setwise coprime");

  else
    puts("not coprime");
}

int main() {

  //    int _; scanf("%d", &_);
  //    while(_--) solve();

  solve();

  return 0;
}