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

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>

using namespace __gnu_pbds;
using namespace std;

typedef long long int ll;
typedef unsigned long long int ull;
typedef long double ld;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    indexed_set;

#define mp make_pair
#define pb push_back
#define pf push_front
#define ss second
#define ff first
#define sz(x) (int)x.size()
#define newl "\\n"
#define vi vector<int>
#define pii pair<int, int>
#define vii vector<pii>
#define vl vector<ll>
#define pll pair<ll, ll>
#define vll vector<pll>
#define coutp cout << fixed << setprecision(12)
#define mem(x, val) memset(x, val, sizeof(x))
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define all(v) (v).begin(), (v).end()

const ld pi = 3.14159265359;
ll INF = 1e18 + 10;
ll MOD = 998244353;
ll mod = 1e9 + 7;

inline ll add(ll a, ll b, ll m) {
  if ((a + b) >= m)
    return (a + b) % m;
  return a + b;
}

inline ll mul(ll a, ll b, ll m) {
  if ((a * b) < m)
    return a * b;
  return (a * b) % m;
}

ll power(ll x, ll y, ll m) {
  ll res = 1;
  x = x % m;
  if (x == 0)
    return 0;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % m;
    y = y >> 1;
    x = (x * x) % m;
  }
  return res;
}

void solve() {
  int n;
  cin >> n;
  vl a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  ll g = a[0];
  for (int i = 1; i < n; i++) {
    g = __gcd(g, a[i]);
  }
  vi sieve(1e6 + 1, 0), prime;

  for (int i = 2; i * i <= 1e6; i++) {
    // if(sieve[i]!=1) prime.pb(i);
    for (int j = i * i; j <= 1e6; j += i) {
      sieve[j] = 1;
    }
  }

  for (int i = 2; i <= 1e6; i++) {
    if (sieve[i] != 1)
      prime.pb(i);
  }

  set<ll> s;
  // cout<<sz(prime)<<newl;
  for (int i = 0; i < n; i++) {
    ll x = a[i];
    for (int p : prime) {
      if (p * p > x)
        break;
      if (x % p == 0) {
        if (s.count(p)) {
          if (g == 1)
            cout << "setwise coprime";
          else
            cout << "not coprime";
          return;
        } else {
          s.insert(p);
        }
      }
      while (x % p == 0) {
        x /= p;
      }
    }
    if (x > 1) {
      if (s.count(x)) {
        if (g == 1)
          cout << "setwise coprime";
        else
          cout << "not coprime";
        return;
      } else {
        s.insert(x);
      }
    }
  }

  cout << "pairwise coprime";
}

int main() {
  fastio;
  int t;

  t = 1;
  // cin >> t;
  // int test=t;
  while (t-- > 0) {
    // cout<<"Case #"<<(test-t)<<": ";
    solve();
    // cout << newl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>

using namespace __gnu_pbds;
using namespace std;

typedef long long int ll;
typedef unsigned long long int ull;
typedef long double ld;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    indexed_set;

#define mp make_pair
#define pb push_back
#define pf push_front
#define ss second
#define ff first
#define sz(x) (int)x.size()
#define newl "\\n"
#define vi vector<int>
#define pii pair<int, int>
#define vii vector<pii>
#define vl vector<ll>
#define pll pair<ll, ll>
#define vll vector<pll>
#define coutp cout << fixed << setprecision(12)
#define mem(x, val) memset(x, val, sizeof(x))
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define all(v) (v).begin(), (v).end()

const ld pi = 3.14159265359;
ll INF = 1e18 + 10;
ll MOD = 998244353;
ll mod = 1e9 + 7;

inline ll add(ll a, ll b, ll m) {
  if ((a + b) >= m)
    return (a + b) % m;
  return a + b;
}

inline ll mul(ll a, ll b, ll m) {
  if ((a * b) < m)
    return a * b;
  return (a * b) % m;
}

ll power(ll x, ll y, ll m) {
  ll res = 1;
  x = x % m;
  if (x == 0)
    return 0;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % m;
    y = y >> 1;
    x = (x * x) % m;
  }
  return res;
}

void solve() {
  int n;
  cin >> n;
  vl a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  ll g = a[0];
  for (int i = 1; i < n; i++) {
    g = __gcd(g, a[i]);
  }
  vi sieve(1e6 + 1, 0), prime;

  for (int i = 2; i * i <= 1e6; i++) {
    // if(sieve[i]!=1) prime.pb(i);
    for (int j = i * i; j <= 1e6; j += i) {
      sieve[j] = 1;
    }
  }

  for (int i = 2; i <= 1e6; i++) {
    if (sieve[i] != 1)
      prime.pb(i);
  }

  set<ll> s;
  for (int i = 0; i < n; i++) {
    ll x = a[i];
    for (int p : prime) {
      if (x == 1)
        break;
      if (x % p == 0) {
        if (s.count(p)) {
          if (g == 1)
            cout << "setwise coprime";
          else
            cout << "not coprime";
          return;
        } else {
          s.insert(p);
        }
      }
      while (x % p == 0) {
        x /= p;
      }
    }
  }

  cout << "pairwise coprime";
}

int main() {
  fastio;
  int t;

  t = 1;
  // cin >> t;
  // int test=t;
  while (t-- > 0) {
    // cout<<"Case #"<<(test-t)<<": ";
    solve();
    // cout << newl;
  }

  return 0;
}