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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <ratio>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;

typedef long long ll;
typedef long double LD;
#define int ll
#define double LD
#define pb push_back
#define mp make_pair
#define REP(i, n) for (int i = 0; i < n; i++)
#define loop(i, n) for (int i = 0; i < n; i++)
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define REPD(i, n) for (int i = n - 1; i >= 0; i--)
#define FORD(i, a, b) for (int i = a; i >= b; i--)
#define remax(a, b) a = max(a, b)
#define remin(a, b) a = min(a, b)
#define mem(a, b) memset(a, b, sizeof a)
#define append push_back
#define all(v) v.begin(), v.end()
#define F first
#define S second
#define print(x) cout << (x) << endl
#define PRECISION(x) cout << fixed << setprecision(x)
#define FAST_IO                                                                \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define Test()                                                                 \\
  {                                                                            \\
    int t;                                                                     \\
    cin >> t;                                                                  \\
    for (int i = 1; i <= t; i++) {                                             \\
      cout << "Case #" << i << ": ";                                           \\
      solve();                                                                 \\
    }                                                                          \\
  }
#define test()                                                                 \\
  {                                                                            \\
    int t;                                                                     \\
    cin >> t;                                                                  \\
    for (int i = 1; i <= t; i++) {                                             \\
      solve();                                                                 \\
    }                                                                          \\
  }
#define vi vector<int>
#define vii vector<pair<int, int>>
#define vci vector<pair<char, int>>
#define vic vector<pair<int, char>>
#define N 2000005
#define pb push_back
const double PI = acos(-1);
const int MOD = 1000000007;
const int FMOD = 998244353;

map<int, int> mp1;

int check(int n) {
  if (n == 1)
    return 0;
  for (int i = 2; i * i <= n; i++) {
    if (n % i == 0)
      return 0;
  }
  return 1;
}
void primeFactors(int n) {
  // Print the number of 2s that divide n
  if (n % 2 == 0)
    mp1[2]++;
  while (n % 2 == 0) {
    n = n / 2;
  }

  // n must be odd at this point. So we can skip
  // one element (Note i = i +2)
  for (int i = 3; i <= sqrt(n); i = i + 2) {
    // While i divides n, print i and divide n
    if (n % i == 0)
      mp1[i]++;
    while (n % i == 0) {
      n = n / i;
    }
  }

  // This condition is to handle the case when n
  // is a prime number greater than 2
  if (n > 2)
    mp1[n]++;
}

void solve() {
  int n;
  cin >> n;
  int a[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  int gcd = a[0];
  for (int i = 1; i < n; i++) {
    gcd = __gcd(a[i], gcd);
  }
  if (gcd != 1) {
    cout << "not coprime" << endl;
  } else {
    for (int i = 0; i < n; i++) {
      if (check(a[i])) {
        mp1[a[i]]++;
      } else {
        primeFactors(a[i]);
      }
    }
    bool ans = false;
    for (auto x : mp1) {

      if (x.second > 1) {
        ans = true;
        break;
      }
    }
    if (ans)
      cout << "setwise coprime" << endl;
    else
      cout << "pairwise coprime" << endl;
  }
}
signed main() {
  //	test();
  solve();
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <ratio>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;

typedef long long ll;
typedef long double LD;
#define int ll
#define double LD
#define pb push_back
#define mp make_pair
#define REP(i, n) for (int i = 0; i < n; i++)
#define loop(i, n) for (int i = 0; i < n; i++)
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define REPD(i, n) for (int i = n - 1; i >= 0; i--)
#define FORD(i, a, b) for (int i = a; i >= b; i--)
#define remax(a, b) a = max(a, b)
#define remin(a, b) a = min(a, b)
#define mem(a, b) memset(a, b, sizeof a)
#define append push_back
#define all(v) v.begin(), v.end()
#define F first
#define S second
#define print(x) cout << (x) << endl
#define PRECISION(x) cout << fixed << setprecision(x)
#define FAST_IO                                                                \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define Test()                                                                 \\
  {                                                                            \\
    int t;                                                                     \\
    cin >> t;                                                                  \\
    for (int i = 1; i <= t; i++) {                                             \\
      cout << "Case #" << i << ": ";                                           \\
      solve();                                                                 \\
    }                                                                          \\
  }
#define test()                                                                 \\
  {                                                                            \\
    int t;                                                                     \\
    cin >> t;                                                                  \\
    for (int i = 1; i <= t; i++) {                                             \\
      solve();                                                                 \\
    }                                                                          \\
  }
#define vi vector<int>
#define vii vector<pair<int, int>>
#define vci vector<pair<char, int>>
#define vic vector<pair<int, char>>
#define N 2000005
#define pb push_back
const double PI = acos(-1);
const int MOD = 1000000007;
const int FMOD = 998244353;

int spf[N];

void sieve() {
  spf[1] = 1;
  for (int i = 2; i < N; i++)

    spf[i] = i;

  for (int i = 4; i < N; i += 2)
    spf[i] = 2;

  for (int i = 3; i * i < N; i++) {
    // checking if i is prime
    if (spf[i] == i) {
      // marking SPF for all numbers divisible by i
      for (int j = i * i; j < N; j += i)

        if (spf[j] == j)
          spf[j] = i;
    }
  }
}

vector<int> getFactorization(int x) {
  vector<int> ret;
  while (x != 1) {
    ret.push_back(spf[x]);
    x = x / spf[x];
  }
  return ret;
}
void solve() {
  sieve();
  int n;
  cin >> n;

  int a[n];
  loop(i, n) cin >> a[i];

  int f = 0;

  map<int, int> mp1, mp2;
  loop(i, n) {
    vector<int> res = getFactorization(a[i]);
    loop(j, res.size()) {
      //    cout<<res[j]<<" ";
      if (mp2[res[j]]) {
        f = 1;
        //  cout<<res[j]<<endl;
      }
      mp1[res[j]] = 1;
    }
    mp2 = mp1;
    //	cout<<endl;
  }
  if (f == 0) {
    cout << "pairwise coprime" << endl;
  } else {
    int p = 0;
    loop(i, n) { p = __gcd(p, a[i]); }
    if (p == 1) {
      cout << "setwise coprime" << endl;
    } else {
      cout << "not coprime" << endl;
    }
  }
}
signed main() {
  //	test();
  solve();
}