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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;

struct Sieve {
  int n;
  vector<int> f, primes;
  Sieve(int n = 1) : n(n), f(n + 1) {
    f[0] = f[1] = -1;
    for (ll i = 2; i <= n; ++i) {
      if (f[i])
        continue;
      primes.push_back(i);
      f[i] = i;
      for (ll j = i * i; j <= n; j += i) {
        if (!f[j])
          f[j] = i;
      }
    }
  }
  bool isPrime(int x) { return f[x] == x; }
  vector<int> factorList(int x) {
    vector<int> res;
    while (x != 1) {
      res.push_back(f[x]);
      x /= f[x];
    }
    return res;
  }
  vector<P> factor(int x) {
    vector<int> fl = factorList(x);
    if (fl.size() == 0)
      return {};
    vector<P> res(1, P(fl[0], 0));
    for (int p : fl) {
      if (res.back().first == p) {
        res.back().second++;
      } else {
        res.emplace_back(p, 1);
      }
    }
    return res;
  }
  vector<pair<ll, int>> factor(ll x) {
    vector<pair<ll, int>> res;
    for (int p : primes) {
      int y = 0;
      while (x % p == 0)
        x /= p, ++y;
      if (y != 0)
        res.emplace_back(p, y);
    }
    if (x != 1)
      res.emplace_back(x, 1);
    return res;
  }
};

void solve(int N, std::vector<int> A) {
  Sieve sieve = Sieve(int(1e6) + 5);
  unordered_map<int, int> mp;
  sort(A.begin(), A.end());

  bool isPairewise = true;
  for (int i = 0; i < N; i++) {
    if (!isPairewise) {
      break;
    }
    vector<P> factors = sieve.factor(A[i]);
    for (P p : factors) {
      if (mp.find(p.first) == mp.end()) {
        mp[p.first]++;
      } else {
        isPairewise = false;
        break;
      }
    }
  }
  if (isPairewise) {
    cout << "pairwise coprime" << endl;
    return;
  }

  // setwise coprime を調査

  vector<unordered_map<int, int>> factors(N);
  for (int i = 0; i < N; i++) {
    vector<P> tempfactors = sieve.factor(A[i]);
    for (P p : tempfactors) {
      factors[i][p.first]++;
    }
  }
  bool isSetwise = true;
  for (auto itr = factors[0].begin(); itr != factors[0].end(); itr++) {
    // cerr << "target =" << target << endl;
    int t = itr->first;
    bool found = true;
    for (int i = 1; i < N; i++) {
      if (factors[i].find(t) == factors[i].end()) {
        found = false;
        break;
      }
    }
    if (found) {
      isSetwise = false;
      break;
    }
  }
  if (isSetwise) {
    cout << "setwise coprime" << endl;
    return;
  }
  cout << "not coprime" << endl;
}

// Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools  (tips:
// You use the default template now. You can remove this line by using your
// custom template)
int main() {
  int N;
  scanf("%d", &N);
  std::vector<int> A(N);
  for (int i = 0; i < N; i++) {
    scanf("%d", &A[i]);
  }
  solve(N, std::move(A));
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, int>;

struct Sieve {
  int n;
  vector<int> f, primes;
  Sieve(int n = 1) : n(n), f(n + 1) {
    f[0] = f[1] = -1;
    for (ll i = 2; i <= n; ++i) {
      if (f[i])
        continue;
      primes.push_back(i);
      f[i] = i;
      for (ll j = i * i; j <= n; j += i) {
        if (!f[j])
          f[j] = i;
      }
    }
  }
  bool isPrime(int x) { return f[x] == x; }
  vector<int> factorList(int x) {
    vector<int> res;
    while (x != 1) {
      res.push_back(f[x]);
      x /= f[x];
    }
    return res;
  }
  vector<P> factor(int x) {
    vector<int> fl = factorList(x);
    if (fl.size() == 0)
      return {};
    vector<P> res(1, P(fl[0], 0));
    for (int p : fl) {
      if (res.back().first == p) {
        res.back().second++;
      } else {
        res.emplace_back(p, 1);
      }
    }
    return res;
  }
  vector<pair<ll, int>> factor(ll x) {
    vector<pair<ll, int>> res;
    for (int p : primes) {
      int y = 0;
      while (x % p == 0)
        x /= p, ++y;
      if (y != 0)
        res.emplace_back(p, y);
    }
    if (x != 1)
      res.emplace_back(x, 1);
    return res;
  }
};

void solve(long long N, std::vector<long long> A) {
  Sieve sieve = Sieve(int(1e6) + 5);
  unordered_map<int, int> mp;

  bool isPairewise = true;
  for (int i = 0; i < N; i++) {
    if (!isPairewise) {
      break;
    }
    vector<P> factors = sieve.factor(A[i]);
    for (P p : factors) {
      if (mp.find(p.first) == mp.end()) {
        mp[p.first]++;
      } else {
        isPairewise = false;
        break;
      }
    }
  }
  if (isPairewise) {
    cout << "pairwise coprime" << endl;
    return;
  }

  // setwise coprime を調査

  vector<vector<ll>> factors(N);
  for (int i = 0; i < N; i++) {
    vector<P> tempfactors = sieve.factor(A[i]);
    for (P p : tempfactors) {
      factors[i].push_back(p.first);
    }
  }
  bool isSetwise = true;
  for (ll target : factors[0]) {
    // cerr << "target =" << target << endl;
    bool found = true;
    for (int i = 1; i < N; i++) {
      bool infound = false;
      for (ll t : factors[i]) {
        if (t == target) {
          infound = true;
          break;
        }
      }
      if (!infound) {
        found = false;
        break;
      }
    }
    if (found) {
      isSetwise = false;
      break;
    }
  }
  if (isSetwise) {
    cout << "setwise coprime" << endl;
    return;
  }
  cout << "not coprime" << endl;
}

// Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools  (tips:
// You use the default template now. You can remove this line by using your
// custom template)
int main() {
  long long N;
  scanf("%lld", &N);
  std::vector<long long> A(N);
  for (int i = 0; i < N; i++) {
    scanf("%lld", &A[i]);
  }
  solve(N, std::move(A));
  return 0;
}