Problem p02574 (C++) — buggy submission, verdict: Time Limit Exceeded

// https://atcoder.jp/contests/abc177/tasks/abc177_e
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define REP(i, n) FOR(i, 0, n)
#define FOR(i, s, n) for (int i = (s), i##_len = (n); i < i##_len; ++i)
#define ALL(obj) (obj).begin(), (obj).end()
#define ALLR(obj) (obj).rbegin(), (obj).rend()
#define CEIL(a, b) ((a - 1) / b + 1)
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }

const int SZ = 1e7;

// エラトステネスの篩
template <int N> struct Prime {
  bitset<N + 1> b;
  constexpr Prime() : b() {
    b.flip();
    b[0] = b[1] = false;
    for (int i = 2; i <= N; ++i) {
      if (b[i]) {
        for (int j = i * i; j <= N; j += i) {
          b[j] = false;
        }
      }
    }
  }
};
auto prime = Prime<SZ>();

void solve() {
  int N;
  cin >> N;
  vector<int> A(N), D(SZ + 1);
  for (auto &x : A) {
    cin >> x;
    // 同じ数が重複するケースを考慮
    D[x]++;
  }
  // trueなら "pairwise coprime"
  bool pc = true;

  REP(i, SZ) {
    // 素数の場合
    if (prime.b[i]) {
      int cnt = 0;
      for (int now = i; now <= SZ; now += i) {
        cnt += D[now];
      }
      // 共通の素数を含むのでgcd(ALL) >= i
      if (cnt >= 2) {
        pc = false;
        break;
      }
    }
  }
  if (pc) {
    cout << "pairwise coprime" << endl;
  } else {
    int g = A[0];
    for (auto &x : A) {
      g = gcd(g, x);
    }
    if (g == 1)
      cout << "setwise coprime" << endl;
    else
      cout << "not coprime" << endl;
  }
}

signed main() {
  cin.tie(nullptr)->sync_with_stdio(false);
  solve();
}


Error: TLE

Fixed submission:

// https://atcoder.jp/contests/abc177/tasks/abc177_e
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define REP(i, n) FOR(i, 0, n)
#define FOR(i, s, n) for (int i = (s), i##_len = (n); i < i##_len; ++i)
#define ALL(obj) (obj).begin(), (obj).end()
#define ALLR(obj) (obj).rbegin(), (obj).rend()
#define CEIL(a, b) ((a - 1) / b + 1)
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }

const int SZ = 1e8;

// constexprな素数列挙(エラトステネスの篩)
// https://259-momone.hatenablog.com/entry/2018/05/06/015026
template <int N> struct Prime {
  bool b[N + 1]; // trueなら素数
  constexpr Prime() : b() {
    fill_n(b, N + 1, true);
    b[0] = b[1] = false;
    for (int i = 2; i <= N; ++i) {
      if (b[i]) {
        for (int j = i * i; j <= N; j += i) {
          b[j] = false;
        }
      }
    }
  }
};
Prime<SZ> prime;

void solve() {
  int N;
  cin >> N;
  vector<int> A(N), D(SZ + 1);
  for (auto &x : A) {
    cin >> x;
    // 同じ数が重複するケースを考慮
    D[x]++;
  }
  // trueなら "pairwise coprime"
  bool pc = true;

  REP(i, SZ) {
    // 素数の場合
    if (prime.b[i]) {
      int cnt = 0;
      for (int now = i; now <= SZ; now += i) {
        cnt += D[now];
      }
      // 共通の素数を含むのでgcd(ALL) >= i
      if (cnt >= 2) {
        pc = false;
        break;
      }
    }
  }
  if (pc) {
    cout << "pairwise coprime" << endl;
  } else {
    int g = A[0];
    for (auto &x : A) {
      g = gcd(g, x);
    }
    if (g == 1)
      cout << "setwise coprime" << endl;
    else
      cout << "not coprime" << endl;
  }
}

signed main() {
  cin.tie(nullptr)->sync_with_stdio(false);
  solve();
}