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

#include <bits/stdc++.h>
#define ll long long int
#define vec vector<ll>
#define mat vector<vector<ll>>

using namespace std;

const ll mod = 1000000007;
const ll inf = LONG_LONG_MAX;
ll dx4[4] = {1, 0, -1, 0};
ll dy4[4] = {0, -1, 0, 1};
ll dx8[8] = {1, 0, -1, 1, -1, 1, 0, -1};
ll dy8[8] = {1, 1, 1, 0, 0, -1, -1, -1};

mat bunkai(ll a) {
  if (a <= 0)
    return {};
  if (a == 1)
    return {{1, 1}};
  vector<ll> f;
  ll b = a;
  for (ll i = 2; i * i <= a; i++) {
    if (b % i == 0) {
      f.push_back(i);
      b = b / i;
      i--;
    }
    if (b == 1)
      break;
  }
  if (b != 1)
    f.push_back(b);

  vector<vector<ll>> u;
  ll p = f[0];
  ll c = 1;
  for (ll i = 1; i < f.size(); i++) {
    if (f[i] == p)
      c++;
    else {
      u.push_back({p, c});
      p = f[i];
      c = 1;
    }
  }
  u.push_back({p, c});

  return u;
}

int main() {

  ll n;
  cin >> n;
  vec a(n);
  for (ll i = 0; i < n; i++)
    cin >> a[i];
  vector<vector<ll>> F(
      1000001); // Fは1~10^6の素因数分解(既に計算したものを入れていく)

  for (ll i = 1; i < 1000001; i++) {
    if (i == 1)
      F[i] = {1};
    else {
      for (ll j = 2; j * j <= i; j++) {
        if (i % j == 0) {
          F[i] = F[i / j];
          F[i].push_back(j);
          break;
        }
      }
      ll b = F[i].size();
      if (b == 0)
        F[i].push_back(i);
      sort(F[i].begin(), F[i].end());
    }
    // cout << i << endl;
  }

  // cout << 5 << endl;

  // cout << 5 << endl;

  vec s(1000001);

  // cout << 5 << endl;

  for (ll i = 0; i < n; i++) {
    // cout << 5 << endl;
    vec f = F[a[i]];
    vector<vector<ll>> u;
    ll p = f[0];
    ll c = 1;
    for (ll i = 1; i < f.size(); i++) {
      if (f[i] == p)
        c++;
      else {
        u.push_back({p, c});
        p = f[i];
        c = 1;
      }
    }
    u.push_back({p, c});
    for (ll j = 0; j < u.size(); j++) {
      s[u[j][0]]++;
    }
    // cout << i << endl;
  }

  // cout << 5 << endl;

  ll m = 0;
  for (ll i = 2; i < 1000001; i++)
    m = max(m, s[i]);

  if (m <= 1)
    cout << "pairwise coprime" << endl;
  else if (m < n)
    cout << "setwise coprime" << endl;
  else
    cout << "not coprime" << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long int
#define vec vector<ll>
#define mat vector<vector<ll>>

using namespace std;

const ll mod = 1000000007;
const ll inf = LONG_LONG_MAX;
ll dx4[4] = {1, 0, -1, 0};
ll dy4[4] = {0, -1, 0, 1};
ll dx8[8] = {1, 0, -1, 1, -1, 1, 0, -1};
ll dy8[8] = {1, 1, 1, 0, 0, -1, -1, -1};

mat bunkai(ll a) {
  if (a <= 0)
    return {};
  if (a == 1)
    return {{1, 1}};
  vector<ll> f;
  ll b = a;
  for (ll i = 2; i * i <= a; i++) {
    if (b % i == 0) {
      f.push_back(i);
      b = b / i;
      i--;
    }
    if (b == 1)
      break;
  }
  if (b != 1)
    f.push_back(b);

  vector<vector<ll>> u;
  ll p = f[0];
  ll c = 1;
  for (ll i = 1; i < f.size(); i++) {
    if (f[i] == p)
      c++;
    else {
      u.push_back({p, c});
      p = f[i];
      c = 1;
    }
  }
  u.push_back({p, c});

  return u;
}

int main() {

  ll n;
  cin >> n;
  vec a(n);
  for (ll i = 0; i < n; i++)
    cin >> a[i];
  vector<vector<vector<ll>>> F(
      1000001); // Fは1~10^6の素因数分解(既に計算したものを入れていく)

  // cout << 5 << endl;

  vec s(1000001);

  // cout << 5 << endl;

  for (ll i = 0; i < n; i++) {
    // cout << 5 << endl;
    mat f;
    ll b = F[a[i]].size();
    if (b > 0) {
      mat v = F[a[i]];
      f = v;
    } else {
      mat v = bunkai(a[i]);
      F[a[i]] = v;
      f = v;
    }
    for (ll j = 0; j < f.size(); j++) {
      s[f[j][0]]++;
    }
  }

  // cout << 5 << endl;

  ll m = 0;
  for (ll i = 2; i < 1000001; i++)
    m = max(m, s[i]);

  if (m <= 1)
    cout << "pairwise coprime" << endl;
  else if (m < n)
    cout << "setwise coprime" << endl;
  else
    cout << "not coprime" << endl;
}