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

// Problem : E - Coprime
// Contest : AtCoder - AtCoder Beginner Contest 177
// URL : https://atcoder.jp/contests/abc177/tasks/abc177_e
// Memory Limit : 1024 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")

#include <bits/stdc++.h>
using namespace std;

/* DEBUGGING */
void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\\'' << x << '\\''; }
void __print(const char *x) { cerr << '\\"' << x << '\\"'; }
void __print(const string &x) { cerr << '\\"' << x << '\\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }

template <typename T, typename V> void __print(const pair<T, V> &x) {
  cerr << '{';
  __print(x.first);
  cerr << ',';
  __print(x.second);
  cerr << '}';
}
template <typename T> void __print(const T &x) {
  int f = 0;
  cerr << '{';
  for (auto &i : x)
    cerr << (f++ ? "," : ""), __print(i);
  cerr << "}";
}
void _print() { cerr << "]\\n"; }
template <typename T, typename... V> void print(T t, V... v) {
  _print(t);
  if (sizeof...(v))
    cerr << ", ";
  _print(v...);
}
#ifndef ONLINE_JUDGE
#define deb(x...)                                                              \\
  cerr << "[" << #x << "] = [";                                                \\
  _print(x)
#else
#define deb(x...)
#endif

/* MACROS */
typedef long long int ll;

typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<pii> vpii;
typedef vector<pll> vpll;

typedef map<int, int> mii;

#define ff first
#define ss second
#define pb push_back
#define IOS                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define lb lower_bound
#define ub upper_bound
#define all(x) (x).begin(), (x).end()
#define lcm(x, y) ((x) * (y) / __gcd(x, y))
#define ps(x, y) fixed << setprecision(y) << x
#define setbit(x) __builtin_popcountll(x)
#define rep(i, a, b) for (int i = a; i < b; ++i)
#define repr(i, a, b) for (int i = a; i >= b; --i)

/* CONSTANTS */
#define PI 3.141592653589793
const ll MOD = 1e9 + 7;
const ll INF = 1000000000;
const ll MAX_N = 1e6 + 2;

// #define cerr if(false)cerr
/* CODE STARTS HERE */

int prime[MAX_N + 1];
void SieveOfEratosthenes(int n) {
  memset(prime, -1, sizeof(prime));
  for (int p = 2; p <= n; p++) {
    if (prime[p] == -1) {
      for (int i = p; i <= n; i += p)
        if (prime[i] == -1)
          prime[i] = p;
    }
  }
}

void solve() {
  SieveOfEratosthenes(MAX_N - 1);
  int n;
  cin >> n;
  vll v(n);
  map<ll, ll> mp;
  rep(i, 0, n) cin >> v[i];

  ll gd = 0, mx_cnt = 0;

  rep(i, 0, n) {
    gd = __gcd(gd, v[i]);
    mp[v[i]]++;
    mx_cnt = max(mx_cnt, mp[v[i]]);
  }

  bool flag = false;
  map<ll, ll> p;

  rep(i, 1, n + 1) {
    int num = v[i - 1];

    while (num != 1) {
      int d = prime[num];
      if (p[d] != i && p[d] != 0) {
        flag = true;
      }
      p[d] = i;
      num /= d;
      if (flag)
        break;
    }
    if (flag)
      break;
  }

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

int main() {
  IOS;
  solve();
}


Error: WA

Fixed submission:


// Problem : E - Coprime
// Contest : AtCoder - AtCoder Beginner Contest 177
// URL : https://atcoder.jp/contests/abc177/tasks/abc177_e
// Memory Limit : 1024 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")

#include <bits/stdc++.h>
using namespace std;

/* DEBUGGING */
void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\\'' << x << '\\''; }
void __print(const char *x) { cerr << '\\"' << x << '\\"'; }
void __print(const string &x) { cerr << '\\"' << x << '\\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }

template <typename T, typename V> void __print(const pair<T, V> &x) {
  cerr << '{';
  __print(x.first);
  cerr << ',';
  __print(x.second);
  cerr << '}';
}
template <typename T> void __print(const T &x) {
  int f = 0;
  cerr << '{';
  for (auto &i : x)
    cerr << (f++ ? "," : ""), __print(i);
  cerr << "}";
}
void _print() { cerr << "]\\n"; }
template <typename T, typename... V> void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v))
    cerr << ", ";
  _print(v...);
}
#ifndef ONLINE_JUDGE
#define deb(x...)                                                              \\
  cerr << "[" << #x << "] = [";                                                \\
  _print(x)
#else
#define deb(x...)
#endif

/* MACROS */
typedef long long int ll;

typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<pii> vpii;
typedef vector<pll> vpll;

typedef map<int, int> mii;

#define ff first
#define ss second
#define pb push_back
#define IOS                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define lb lower_bound
#define ub upper_bound
#define all(x) (x).begin(), (x).end()
#define lcm(x, y) ((x) * (y) / __gcd(x, y))
#define ps(x, y) fixed << setprecision(y) << x
#define setbit(x) __builtin_popcountll(x)
#define rep(i, a, b) for (int i = a; i < b; ++i)
#define repr(i, a, b) for (int i = a; i >= b; --i)

/* CONSTANTS */
#define PI 3.141592653589793
const ll MOD = 1e9 + 7;
const ll INF = 1000000000;
const ll MAX_N = 2e5 + 2;

// #define cerr if(false)cerr
/* CODE STARTS HERE */

int prime[MAX_N];
void SieveOfEratosthenes(int n) {
  memset(prime, -1, sizeof(prime));
  for (int p = 2; p <= n; p++) {
    if (prime[p] == -1) {
      for (int i = p; i <= n; i += p)
        if (prime[i] == -1)
          prime[i] = p;
    }
  }
}

void solve() {
  SieveOfEratosthenes(MAX_N - 1);
  int n;
  cin >> n;
  vll v(n);
  ll gd = 0;
  rep(i, 0, n) {
    cin >> v[i];
    gd = __gcd(gd, v[i]);
  }

  bool flag = false;
  map<ll, ll> mp;

  rep(i, 1, n + 1) {
    ll num = v[i - 1];

    while (num != 1) {
      ll d = prime[num];
      if (mp[d] != i && mp[d] != 0) {
        flag = true;
      }
      mp[d] = i;
      num /= d;
      if (flag)
        break;
    }
    if (flag)
      break;
  }

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

int main() {
  IOS;
  solve();
}