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

#include <bits/stdc++.h>
using namespace std;

// #define int long long
#define flash                                                                  \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define pb push_back
#define all(x) begin(x), end(x)

int mod = 1e9 + 7;
int inf = 4e18;
const int N = 5e5 + 5;
#define MAXN 1000005

// stores smallest prime factor for every number
int spf[MAXN + 5];

// Calculating SPF (Smallest Prime Factor) for every
// number till MAXN.
// Time Complexity : O(nloglogn)
void sieve() {
  spf[1] = 1;
  for (int i = 2; i < MAXN; i++)

    // marking smallest prime factor for every
    // number to be itself.
    spf[i] = i;

  // separately marking spf for every even
  // number as 2
  for (int i = 4; i < MAXN; i += 2)
    spf[i] = 2;

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

        // marking spf[j] if it is not
        // previously marked
        if (spf[j] == j)
          spf[j] = i;
    }
  }
}

// A O(log n) function returning primefactorization
// by dividing by smallest prime factor at every step
unordered_set<int> temp[MAXN];
void getFactorization(int x, int i) {
  int temp1 = x;
  while (x != 1) {
    temp[spf[x]].insert(temp1);
    x = x / spf[x];
  }
}
int freq[MAXN];
signed main() {
  flash int tt = 1;
  // cin>>tt;
  while (tt--) {
    int n;
    cin >> n;
    sieve();
    int arr[n];
    int maxx = 0;
    bool flag = false;
    for (int i = 0; i < n; ++i) {
      cin >> arr[i];
      freq[arr[i]]++;
      if (arr[i] > 1 and freq[arr[i]] > 1)
        flag = true;
      maxx = max(maxx, arr[i]);
      getFactorization(arr[i], i);
    }
    // cout<<spf[3]<<"\\n";
    int gcdd = arr[0];

    for (int i = 2; i <= maxx; ++i) {
      if (temp[i].size() > 1) {
        flag = true;
        // cout<<i;
        break;
      }
    }
    for (int i = 1; i < n; ++i) {
      gcdd = __gcd(gcdd, arr[i]);
    }
    if (!flag) {
      cout << "pairwise coprime";
    } else if (gcdd == 1) {
      cout << "setwise coprime";
    } else {
      cout << "not coprime";
    }
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

// #define int long long
#define flash                                                                  \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define pb push_back
#define all(x) begin(x), end(x)

int mod = 1e9 + 7;
int inf = 4e18;
const int N = 5e5 + 5;
#define MAXN 1000005

// stores smallest prime factor for every number
int spf[MAXN + 5];

// Calculating SPF (Smallest Prime Factor) for every
// number till MAXN.
// Time Complexity : O(nloglogn)
void sieve() {
  spf[1] = 1;
  for (int i = 2; i < MAXN; i++)

    // marking smallest prime factor for every
    // number to be itself.
    spf[i] = i;

  // separately marking spf for every even
  // number as 2
  for (int i = 4; i < MAXN; i += 2)
    spf[i] = 2;

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

        // marking spf[j] if it is not
        // previously marked
        if (spf[j] == j)
          spf[j] = i;
    }
  }
}

// A O(log n) function returning primefactorization
// by dividing by smallest prime factor at every step
set<pair<int, int>> temp[MAXN];
void getFactorization(int x, int i) {
  int temp1 = x;
  while (x != 1) {
    temp[spf[x]].insert(make_pair(temp1, i));
    x = x / spf[x];
  }
}

signed main() {
  flash int tt = 1;
  // cin>>tt;
  while (tt--) {
    int n;
    cin >> n;
    sieve();
    int arr[n];
    int maxx = 0;
    for (int i = 0; i < n; ++i) {
      cin >> arr[i];
      maxx = max(maxx, arr[i]);
      getFactorization(arr[i], i);
    }
    // cout<<spf[3]<<"\\n";
    int gcdd = arr[0];
    bool flag = false;
    for (int i = 2; i <= maxx; ++i) {
      if (temp[i].size() > 1) {
        flag = true;
        // cout<<i;
        break;
      }
    }
    for (int i = 1; i < n; ++i) {
      gcdd = __gcd(gcdd, arr[i]);
    }
    if (!flag) {
      cout << "pairwise coprime";
    } else if (gcdd == 1) {
      cout << "setwise coprime";
    } else {
      cout << "not coprime";
    }
  }
  return 0;
}