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

#pragma GCC optimize("O3")
#pragma GCC target("sse4")
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
using namespace std;
#define FAST                                                                   \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set                                                            \\
  tree<int, null_type, less<int>, rb_tree_tag,                                 \\
       tree_order_statistics_node_update>
#define ll long long int
#define ar array
#define pb push_back
#define fi(a, b) for (int i = a; i < (b); i++)
#define fj(a, b) for (int j = a; j < (b); j++)
#define fk(a, b) for (int k = a; k < (b); k++)
#define lb lower_bound
#define ub upper_bound
// hash base 177013

int main() {
  FAST;
  // #ifndef ONLINE_JUDGE
  //	freopen("input.txt","r",stdin);
  //	freopen("output.txt","w",stdout);
  // #endif
  // ll testcase;
  // cin>>testcase;
  // while(testcase--)
  {
    int n;
    cin >> n;
    int a[n];
    vector<int> m(1000006);
    fi(0, n) {
      cin >> a[i];
      m[a[i]]++;
    }
    bool pw = true;
    for (ll i = 2; i <= 1000005; i++) {
      int count = 0;
      for (ll j = i; j <= 1000005; j += i) {
        count += m[j];
      }
      if (count > 1) {
        pw = false;
        break;
      }
    }
    if (pw) {
      cout << "pairwise coprime\\n";
      return 0;
    }

    int temp = a[0];
    fi(1, n) { temp = __gcd(temp, a[i]); }
    if (temp == 1) {
      cout << "setwise coprime\\n";
    } else {
      cout << "not coprime\\n";
    }
  }
}


Error: TLE

Fixed submission:

#pragma GCC optimize("O3")
#pragma GCC target("sse4")
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
using namespace std;
#define FAST                                                                   \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set                                                            \\
  tree<int, null_type, less<int>, rb_tree_tag,                                 \\
       tree_order_statistics_node_update>
#define ll long long int
#define ar array
#define pb push_back
#define fi(a, b) for (int i = a; i < (b); i++)
#define fj(a, b) for (int j = a; j < (b); j++)
#define fk(a, b) for (int k = a; k < (b); k++)
#define lb lower_bound
#define ub upper_bound
// hash base 177013

const int mod = 1e9 + 7;

int main() {
  FAST;
  // #ifndef ONLINE_JUDGE
  //	freopen("input.txt","r",stdin);
  //	freopen("output.txt","w",stdout);
  // #endif
  // ll testcase;
  // cin>>testcase;
  // while(testcase--)
  {
    int n;
    cin >> n;
    int a[n];
    fi(0, n) { cin >> a[i]; }
    bool pair = true;
    fi(0, n) {
      int temp = a[i];
      fj(i + 1, n) { temp = __gcd(temp, a[j]); }
      if (temp != 1 && i != n - 1) {
        pair = false;
        break;
      }
    }
    if (pair) {
      cout << "pairwise coprime\\n";
      return 0;
    }
    int temp = a[0];
    fi(1, n) { temp = __gcd(temp, a[i]); }
    if (temp == 1) {
      cout << "setwise coprime\\n";
    } else {
      cout << "not coprime\\n";
    }
  }
}