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

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

#define MOD (long long)(1e9 + 7)
#define INF (1LL << 60)
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define rep1(i, n) for (ll i = 1; i <= (n); i++)

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

// 最大公約数
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

// mod m におけるa の逆元
ll modinv(ll a, ll m) {
  ll b = m, u = 1, v = 0;
  while (b) {
    ll t = a / b;
    a -= t * b;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  u %= m;
  if (u < 0)
    u += m;
  return u;
}

// 素因数分解
vector<pair<ll, ll>> prim;
void pf(ll n) {
  ll s = sqrt(n);
  ll r = 0;
  for (ll i = 2; i <= s; i++) {
    if ((n % i) == 0) {
      r = 0;
      do {
        r++;
        n = n / i;
      } while ((n % i) == 0);
      prim.push_back({i, r});
    }
  }
  if (n > s) {
    prim.push_back({n, 1});
  }
}

ll firstp(ll n) {
  for (ll i = 2; i * i <= n; i++) {
    if (n % i == 0)
      return i;
  }
  return n;
}

void solve() {
  ll N;
  cin >> N;
  vector<ll> a(N);
  rep(i, N) cin >> a[i];
  //  cerr << firstp(1) << endl;
  ll maxa = 0;
  rep(i, N) { maxa = max(maxa, a[i]); }

  vector<ll> D(maxa + 1);
  rep1(i, maxa) { D[i] = firstp(i); }
  vector<ll> pc(maxa + 1, 0);
  rep(i, N) {
    //            cerr << a[i] << "] ";
    ll b = a[i];
    while (b > 1) {
      //      cerr << D[b] << " ";
      ll p1 = D[b];
      pc[p1]++;
      while (b % p1 == 0)
        b /= p1;
    }
    //    cerr << endl;
  }
  ll pcmax = 0;
  for (ll i = 2; i <= maxa; i++) {
    pcmax = max(pcmax, pc[i]);
  }
  //  cerr << "pcmax:" << pcmax << endl;
  if (pcmax <= 1)
    cout << "pairwise coprime\\n";
  else if (pcmax < N)
    cout << "setwise coprime\\n";
  else
    cout << "not coprime\\n";
}

int main(void) {
  // ll t; cin >> t; rep(i, t)
  solve();
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

#define MOD (long long)(1e9 + 7)
#define INF (1LL << 60)
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define rep1(i, n) for (ll i = 1; i <= (n); i++)

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

// 最大公約数
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

// mod m におけるa の逆元
ll modinv(ll a, ll m) {
  ll b = m, u = 1, v = 0;
  while (b) {
    ll t = a / b;
    a -= t * b;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  u %= m;
  if (u < 0)
    u += m;
  return u;
}

// 素因数分解
vector<pair<ll, ll>> prim;
void pf(ll n) {
  ll s = sqrt(n);
  ll r = 0;
  for (ll i = 2; i <= s; i++) {
    if ((n % i) == 0) {
      r = 0;
      do {
        r++;
        n = n / i;
      } while ((n % i) == 0);
      prim.push_back({i, r});
    }
  }
  if (n > s) {
    prim.push_back({n, 1});
  }
}

ll firstp(ll n) {
  for (ll i = 2; i * i <= n; i++) {
    if (n % i == 0)
      return i;
  }
  return n;
}

void solve() {
  ll N;
  cin >> N;
  vector<ll> a(N);
  rep(i, N) cin >> a[i];
  //  cerr << firstp(1) << endl;
  ll maxa = 0;
  rep(i, N) { maxa = max(maxa, a[i]); }

  vector<ll> D(maxa + 1);
  rep1(i, maxa) { D[i] = firstp(i); }
  vector<ll> pc(maxa + 1, 0);
  rep(i, N) {
    cerr << a[i] << "] ";
    ll b = a[i];
    while (b > 1) {
      cerr << D[b] << " ";
      ll p1 = D[b];
      pc[p1]++;
      while (b % p1 == 0)
        b /= p1;
    }
    cerr << endl;
  }
  ll pcmax = 0;
  for (ll i = 2; i <= maxa; i++) {
    pcmax = max(pcmax, pc[i]);
  }
  //  cerr << "pcmax:" << pcmax << endl;
  if (pcmax <= 1)
    cout << "pairwise coprime\\n";
  else if (pcmax < N)
    cout << "setwise coprime\\n";
  else
    cout << "not coprime\\n";
}

int main(void) {
  // ll t; cin >> t; rep(i, t)
  solve();
}