Problem p02574 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define Rep(i, a, b) for (ll i = a; i <= b; i++)
#define repr(i, a, b) for (ll i = b - 1; i >= a; i--)
#define _GLIBCXX_DEBUG
#define Vl vector<ll>
#define Vs vector<string>
#define Vp vector<pair<ll, ll>>
using ll = long long;
#define ALL(v) (v).begin(), (v).end()
#define endl "\n"
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define co(x) cout << x << endl
#define coel cout << endl
#define pb push_back
#define sz(v) ((ll)(v).size())
const double pi = acos(-1.0);
const ll MOD = 1e9 + 7;
// const ll INF = 1LL << 60;
const ll INF = 100000000000000;
#define pp pair<ll, pair<ll, ll>>
// #define fi first
// #define se second

void printv(Vl v) {
  for (int i = 0; i < v.size(); i++)
    cout << v[i] << " \n"[i == v.size() - 1];
}

/*--------------------------------------------------------------------------------------


--------------------------------------------------------------------------------------*/

// O(log (N)) の素因数分解
// エラストテネスの篩を使った素因数分解
// 前処理　O(N loglog(N))

class smart_sieve {
  ll L, R, M;
  vector<int> small;        // 小さい篩
  vector<vector<ll>> large; // 大きい篩
  vector<ll> aux;           // aux[i] := large[i] の素因数の積

public:
  smart_sieve(ll L, ll R) : L(L), R(R), M(sqrt(R) + 1) {
    small.resize(M);
    iota(small.begin(), small.end(), 0);
    large.resize(R - L);
    aux.assign(R - L, 1);

    for (ll i = 2; i * i < R; ++i) {
      if (small[i] < i)
        continue;
      small[i] = i;
      for (ll j = i * i; j < M; j += i)
        if (small[j] == j)
          small[j] = i;

      for (ll j = (L + i - 1) / i * i; j < R; j += i) {
        ll k = j;
        do {
          if (aux[j - L] * aux[j - L] > R)
            break;

          large[j - L].push_back(i);
          aux[j - L] *= i;
          k /= i;
        } while (k % i == 0);
      }
    }
  }

  vector<ll> factor(ll n) {
    assert(L <= n && n < R);
    vector<ll> res = large[n - L];
    n /= aux[n - L];
    if (n >= M) {
      res.push_back(n);
      return res;
    }
    while (n > 1) {
      res.push_back(small[n]);
      n /= small[n];
    }
    return res;
  }
};

ll b[1000005];

int main() {
  smart_sieve ss(1, 1000000); // 前処理

  ll n;
  cin >> n;
  Vl a(n);
  rep(i, 0, n) cin >> a[i];
  rep(i, 0, n) {
    Vl fac = ss.factor(a[i]);
    set<ll> factor;
    for (auto it : fac)
      factor.insert(it);
    for (auto it : factor)
      b[it]++;
  }

  int f = 0;
  Rep(i, 1, 1000000) {
    if (b[i] >= 2)
      f = 1;
    if (b[i] == n) {
      co("not coprime");
      return 0;
    }
  }
  if (f == 0)
    co("pairwise coprime");
  if (f == 1)
    co("setwise coprime");

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define Rep(i, a, b) for (ll i = a; i <= b; i++)
#define repr(i, a, b) for (ll i = b - 1; i >= a; i--)
#define _GLIBCXX_DEBUG
#define Vl vector<ll>
#define Vs vector<string>
#define Vp vector<pair<ll, ll>>
using ll = long long;
#define ALL(v) (v).begin(), (v).end()
#define endl "\n"
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define co(x) cout << x << endl
#define coel cout << endl
#define pb push_back
#define sz(v) ((ll)(v).size())
const double pi = acos(-1.0);
const ll MOD = 1e9 + 7;
// const ll INF = 1LL << 60;
const ll INF = 100000000000000;
#define pp pair<ll, pair<ll, ll>>
// #define fi first
// #define se second

void printv(Vl v) {
  for (int i = 0; i < v.size(); i++)
    cout << v[i] << " \n"[i == v.size() - 1];
}

/*--------------------------------------------------------------------------------------


--------------------------------------------------------------------------------------*/

// O(log (N)) の素因数分解
// エラストテネスの篩を使った素因数分解
// 前処理　O(N loglog(N))

class smart_sieve {
  ll L, R, M;
  vector<int> small;        // 小さい篩
  vector<vector<ll>> large; // 大きい篩
  vector<ll> aux;           // aux[i] := large[i] の素因数の積

public:
  smart_sieve(ll L, ll R) : L(L), R(R), M(sqrt(R) + 1) {
    small.resize(M);
    iota(small.begin(), small.end(), 0);
    large.resize(R - L);
    aux.assign(R - L, 1);

    for (ll i = 2; i * i < R; ++i) {
      if (small[i] < i)
        continue;
      small[i] = i;
      for (ll j = i * i; j < M; j += i)
        if (small[j] == j)
          small[j] = i;

      for (ll j = (L + i - 1) / i * i; j < R; j += i) {
        ll k = j;
        do {
          if (aux[j - L] * aux[j - L] > R)
            break;

          large[j - L].push_back(i);
          aux[j - L] *= i;
          k /= i;
        } while (k % i == 0);
      }
    }
  }

  vector<ll> factor(ll n) {
    assert(L <= n && n < R);
    vector<ll> res = large[n - L];
    n /= aux[n - L];
    if (n >= M) {
      res.push_back(n);
      return res;
    }
    while (n > 1) {
      res.push_back(small[n]);
      n /= small[n];
    }
    return res;
  }
};

ll b[1000005];

int main() {
  smart_sieve ss(1, 1000001); // 前処理

  ll n;
  cin >> n;
  Vl a(n);
  rep(i, 0, n) cin >> a[i];
  rep(i, 0, n) {
    Vl fac = ss.factor(a[i]);
    set<ll> factor;
    for (auto it : fac)
      factor.insert(it);
    for (auto it : factor)
      b[it]++;
  }

  int f = 0;
  Rep(i, 1, 1000000) {
    if (b[i] >= 2)
      f = 1;
    if (b[i] == n) {
      co("not coprime");
      return 0;
    }
  }
  if (f == 0)
    co("pairwise coprime");
  if (f == 1)
    co("setwise coprime");

  return 0;
}