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

#include <bits/stdc++.h>
#include <stdlib.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;
typedef vector<double> Vec;
typedef vector<Vec> Mat;
typedef pair<ll, ll> P;
typedef pair<double, ll> Pd;
typedef pair<double, double> PD;
typedef priority_queue<P, vector<P>, greater<P>> P_queue;
typedef priority_queue<Pd, vector<Pd>, greater<Pd>> Pd_queue;

const ll MOD = 998244353;
const ll mod = 1000000007;
const ll INF = 1e15;
const double DEL = 1e-6;

#define _GLIBCXX_DEBUG
#define REP(i, a, b) for (int i = (int)a; i < (int)b; i++)
#define rep(i, n) REP(i, 0, n)
#define pb push_back
#define mp make_pair
#define ALL(a) a.begin(), a.end()
#define SORT(a) sort(ALL(a))
#define U_ERASE(V) V.erase(unique(ALL(V)), V.end());

ll yaku(ll a, ll b) {
  if (a > b)
    return yaku(b, a);
  return b % a ? yaku(b % a, a) : a;
}

vec Sosu;
void Eratosthenes(ll N, vec &ret) {
  vector<bool> a(N + 1, true);
  a.at(0) = false;
  a.at(1) = false;
  REP(i, 2, N + 1) {
    if (a.at(i))
      for (ll k = 2 * i; k <= N; k += i)
        a.at(k) = false;
  }
  rep(i, N + 1) if (a.at(i)) ret.pb(i);
  return;
}

void init(ll N) {
  ll BIG = N, SMALL = 0;
  while (BIG > SMALL + 1) {
    ll MID = (BIG + SMALL) / 2;
    if (MID * MID >= N)
      BIG = MID;
    else
      SMALL = MID;
  }
  // cout<<"era"<<N<<' '<<BIG+3<<endl;
  Eratosthenes(BIG + 100, Sosu);
}

const ll MAXN = 1000010;

bool Toku(vector<bool> &used, ll N) {
  if (N == 1)
    return true;
  for (int i = 0; Sosu.at(i) * Sosu.at(i) <= N; i++) {
    ll k = Sosu.at(i);
    ll count = 0;
    while (true) {
      if (N % k != 0)
        break;
      count++;
      N /= k;
    }
    if (count != 0) {
      if (used[k])
        return false;
      else
        used[k] = true;
    }
  }
  if (N == 1)
    return true;
  if (used[N])
    return false;
  used[N] = true;
  return true;
}

string Solve() {
  ll N;
  cin >> N;
  vec d(N);
  rep(i, N) cin >> d[i];
  ll memo = d[0];
  REP(i, 1, N) {
    memo = yaku(d[i], memo);
    if (memo == 1)
      break;
  }
  if (memo > 1)
    return "not coprime";

  init(MAXN);
  // rep(i,Sosu.size()) cout<<Sosu[i]<<endl;
  vector<bool> used(MAXN, false);
  rep(i, N) if (!Toku(used, d[i])) return "setwise coprime";
  return "pairwise coprime";
}

int main() { cout << Solve() << endl; }

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <stdlib.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;
typedef vector<double> Vec;
typedef vector<Vec> Mat;
typedef pair<ll, ll> P;
typedef pair<double, ll> Pd;
typedef pair<double, double> PD;
typedef priority_queue<P, vector<P>, greater<P>> P_queue;
typedef priority_queue<Pd, vector<Pd>, greater<Pd>> Pd_queue;

const ll MOD = 998244353;
const ll mod = 1000000007;
const ll INF = 1e15;
const double DEL = 1e-6;

#define _GLIBCXX_DEBUG
#define REP(i, a, b) for (int i = (int)a; i < (int)b; i++)
#define rep(i, n) REP(i, 0, n)
#define pb push_back
#define mp make_pair
#define ALL(a) a.begin(), a.end()
#define SORT(a) sort(ALL(a))
#define U_ERASE(V) V.erase(unique(ALL(V)), V.end());

ll yaku(ll a, ll b) {
  if (a > b)
    return yaku(b, a);
  return b % a ? yaku(b % a, a) : a;
}

vec Sosu;
void Eratosthenes(ll N, vec &ret) {
  vector<bool> a(N + 1, true);
  a.at(0) = false;
  a.at(1) = false;
  REP(i, 2, N + 1) {
    if (a.at(i))
      for (ll k = 2 * i; k <= N; k += i)
        a.at(k) = false;
  }
  rep(i, N + 1) if (a.at(i)) ret.pb(i);
  return;
}

void init(ll N) {
  ll BIG = N, SMALL = 0;
  while (BIG > SMALL + 1) {
    ll MID = (BIG + SMALL) / 2;
    if (MID * MID >= N)
      BIG = MID;
    else
      SMALL = MID;
  }
  // cout<<"era"<<N<<' '<<BIG+3<<endl;
  Eratosthenes(BIG + 3, Sosu);
}

const ll MAXN = 1000010;

bool Toku(vector<bool> &used, ll N) {
  if (N == 1)
    return true;
  for (int i = 0; Sosu.at(i) * Sosu.at(i) <= N; i++) {
    ll k = Sosu.at(i);
    ll count = 0;
    while (true) {
      if (N % k != 0)
        break;
      count++;
      N /= k;
    }
    if (count != 0) {
      if (used[k])
        return false;
      else
        used[k] = true;
    }
  }
  if (used[N])
    return false;
  used[N] = true;
  return true;
}

string Solve() {
  ll N;
  cin >> N;
  vec d(N);
  rep(i, N) cin >> d[i];
  ll memo = d[0];
  REP(i, 1, N) {
    memo = yaku(d[i], memo);
    if (memo == 1)
      break;
  }
  if (memo > 1)
    return "not coprime";

  init(MAXN);
  // rep(i,Sosu.size()) cout<<Sosu[i]<<endl;
  vector<bool> used(MAXN, false);
  rep(i, N) if (!Toku(used, d[i])) return "setwise coprime";
  return "pairwise coprime";
}

int main() { cout << Solve() << endl; }