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

#define ll long long
#define pb push_back
#define make_pair mp
#define first f
#define second s
#define upper_bound ub
#define lower_bound lb
#define MOD 1000000007
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

#define ordered_set                                                            \\
  tree<pair<ll, ll>, null_type, less<pair<ll, ll>>, rb_tree_tag,               \\
       tree_order_statistics_node_update>                                      \\
      ll fn

void solve() {
  ll n;
  cin >> n;
  ll a[n + 1];
  vector<ll> primes;
  bool isprime[1006];
  memset(isprime, 1, sizeof(isprime));
  // memset(freq,1,sizeof(freq));
  isprime[2] = 1;
  for (ll i = 2; i * i <= 1000; i++) {
    if (isprime[i]) {
      for (ll j = 2 * i; j <= 1000; j += i) {
        isprime[j] = 0;
      }
    }
  }
  unordered_map<ll, ll> m;
  for (ll i = 2; i <= 1000; i++) {
    if (isprime[i]) {
      primes.pb(i);
      m[i] = 0;
    }
  }

  ll gcd = 0;
  bool f = 1;
  for (ll i = 1; i <= n; i++) {
    cin >> a[i];
    if (gcd == 0) {
      gcd = a[i];
    } else {
      gcd = __gcd(gcd, a[i]);
    }
    for (ll j = 0; j < primes.size() && primes[j] <= a[i] && f == 1; j++) {
      if (a[i] % primes[j] == 0) {
        m[primes[j]]++;
        if (m[primes[j]] >= 2) {
          f = 0;
        }
        while (a[i] % primes[j] == 0) {
          a[i] /= primes[j];
        }
      }
    }
    if (a[i] > 1) {
      if (m.find(a[i]) != m.end()) {
        m[a[i]]++;
        if (m[a[i]] >= 2) {
          f = 0;
        }
      } else {
        m[a[i]] = 1;
      }
    }
  }
  if (f) {
    cout << "pairwise coprime";
    return;
  }
  if (gcd == 1) {
    cout << "setwise coprime";
    return;
  }
  cout << "not coprime";
  return;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll t;
  // cin>>t;
  t = 1;
  while (t) {
    solve();
    t--;
    // cout<<"end of test case"<<endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#define ll long long
#define pb push_back
#define make_pair mp
#define first f
#define second s
#define upper_bound ub
#define lower_bound lb
#define MOD 1000000007
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

#define ordered_set                                                            \\
  tree<pair<ll, ll>, null_type, less<pair<ll, ll>>, rb_tree_tag,               \\
       tree_order_statistics_node_update>                                      \\
      ll fn

void solve() {
  ll n;
  cin >> n;
  ll a[n + 1], freq[1000006];
  vector<ll> primes;
  bool isprime[1000006];
  memset(isprime, 1, sizeof(isprime));
  // memset(freq,1,sizeof(freq));
  isprime[2] = 1;
  for (ll i = 2; i * i <= 1000000; i++) {
    if (isprime[i]) {
      for (ll j = 2 * i; j <= 1000000; j += i) {
        isprime[j] = 0;
      }
    }
  }
  unordered_map<ll, ll> m;
  for (ll i = 2; i <= 1000000; i++) {
    if (isprime[i]) {
      primes.pb(i);
      m[i] = 0;
    }
  }

  ll gcd = 0;
  bool f = 1;
  for (ll i = 1; i <= n; i++) {
    cin >> a[i];
    if (gcd == 0) {
      gcd = a[i];
    } else {
      gcd = __gcd(gcd, a[i]);
    }
    for (ll j = 0; j < primes.size() && primes[j] <= a[i] && f == 1; j++) {
      if (a[i] % primes[j] == 0) {
        m[primes[j]]++;
        if (m[primes[j]] >= 2) {
          f = 0;
        }
      }
    }
  }
  if (f) {
    cout << "pairwise coprime";
    return;
  }
  if (gcd == 1) {
    cout << "setwise coprime";
    return;
  }
  cout << "not coprime";
  return;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll t;
  // cin>>t;
  t = 1;
  while (t) {
    solve();
    t--;
    // cout<<"end of test case"<<endl;
  }
  return 0;
}