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

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define pb push_back
#define pf push_front
#define all(t) t.begin(), t.end()
#define inrange(i, a, b) (((i) >= min((a), (b))) && ((i) <= max((a), (b))))
typedef vector<ll> vi;
typedef pair<ll, ll> pii;
#define fi first
#define se second
#define upb upper_bound
#define lwb lower_bound
#define show(x) cout << #x << " is " << x << "\\n";
const ll inf = 2e18;
const ll mod = 1e9 + 7;
const ld pi = 3.141592653589793238462643383279502884;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

void print(ll a[], ll n) {
  for (ll i = 0; i < n; i++) {
    cout << a[i] << " ";
  }
  cout << "\\n";
}
ll power(ll x, ll y) {
  if (y <= 0)
    return 1;
  ll ans = 1;
  x %= mod;
  while (y) {
    if (y & 1)
      ans = (x * ans) % mod;
    x = (x * x) % mod;
    y >>= 1;
  }
  return ans;
}
ll modInverse(ll n) { return power(n, mod - 2); }
inline ll mul(ll a, ll b) { return (a * b) % mod; }
inline ll sub(ll a, ll b) {
  ll c = (a - b);
  if (c < 0)
    c += mod;
  return c;
}
inline ll add(ll a, ll b) {
  ll c = (a + b);
  if (c >= mod)
    c -= mod;
  return c;
}
inline ll divi(ll a, ll b) { return mul(a, modInverse(b)); }

//------------------------------------------------------------------------------------------

// const ll N = 2e5 + 1;
// vector<ll> adj[N];
// bool visited[N];

map<ll, ll> prm(ll n) {
  map<ll, ll> op;
  if (n % 2 == 0) {
    op[2]++;
    while (n % 2 == 0) {
      n = n / 2;
    }
  }

  for (int i = 3; i <= sqrt(n); i = i + 2) {
    if (n % i == 0) {
      op[i]++;
      while (n % i == 0) {
        n = n / i;
      }
    }
  }

  if (n > 2)
    op[n]++;

  return op;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cout << fixed << setprecision(10);

  ll n;
  cin >> n;
  ll a[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    //		a[i] = prm(a[i]);
  }
  ll g = a[0];
  for (int i = 1; i < n; i++) {
    g = __gcd(g, a[i]);
  }
  int flag = 0;

  map<ll, ll> pref;
  map<ll, ll> op;
  for (int i = n - 2; i >= 0; i--) {

    ll b = a[i + 1];
    op = prm(b);
    for (auto it : op) {
      if (pref[it.fi])
        flag = 1;
      pref[it.fi]++;
    }
    if (flag)
      break;
  }

  if (flag == 0) {
    cout << "pairwise coprime";
  } else if (g == 1) {
    cout << "setwise coprime";
  } else {
    cout << "not coprime";
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define pb push_back
#define pf push_front
#define all(t) t.begin(), t.end()
#define inrange(i, a, b) (((i) >= min((a), (b))) && ((i) <= max((a), (b))))
typedef vector<ll> vi;
typedef pair<ll, ll> pii;
#define fi first
#define se second
#define upb upper_bound
#define lwb lower_bound
#define show(x) cout << #x << " is " << x << "\\n";
const ll inf = 2e18;
const ll mod = 1e9 + 7;
const ld pi = 3.141592653589793238462643383279502884;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

void print(ll a[], ll n) {
  for (ll i = 0; i < n; i++) {
    cout << a[i] << " ";
  }
  cout << "\\n";
}
ll power(ll x, ll y) {
  if (y <= 0)
    return 1;
  ll ans = 1;
  x %= mod;
  while (y) {
    if (y & 1)
      ans = (x * ans) % mod;
    x = (x * x) % mod;
    y >>= 1;
  }
  return ans;
}
ll modInverse(ll n) { return power(n, mod - 2); }
inline ll mul(ll a, ll b) { return (a * b) % mod; }
inline ll sub(ll a, ll b) {
  ll c = (a - b);
  if (c < 0)
    c += mod;
  return c;
}
inline ll add(ll a, ll b) {
  ll c = (a + b);
  if (c >= mod)
    c -= mod;
  return c;
}
inline ll divi(ll a, ll b) { return mul(a, modInverse(b)); }

//------------------------------------------------------------------------------------------

// const ll N = 2e5 + 1;
// vector<ll> adj[N];
// bool visited[N];

map<ll, ll> prm(ll n) {
  map<ll, ll> op;
  if (n % 2 == 0) {
    op[2]++;
    while (n % 2 == 0) {
      n = n / 2;
    }
  }

  for (int i = 3; i <= sqrt(n); i = i + 2) {
    if (n % i == 0) {
      op[i]++;
      while (n % i == 0) {
        n = n / i;
      }
    }
  }

  if (n > 2)
    op[n]++;

  return op;
}
map<ll, ll> lcm(map<ll, ll> a, ll b, int &flag) {
  map<ll, ll> op;
  op = prm(b);
  for (auto it : a) {
    if (op[it.fi])
      flag = 1;
    op[it.fi]++;
  }
  return op;
}
ll gcd(map<ll, ll> a, ll b) {
  int flag = 0;

  for (auto it : a) {
    if (b % it.fi == 0)
      flag = 1;
  }

  return flag;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cout << fixed << setprecision(10);

  ll n;
  cin >> n;
  ll a[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    //		a[i] = prm(a[i]);
  }
  ll g = a[0];
  for (int i = 1; i < n; i++) {
    g = __gcd(g, a[i]);
  }
  int flag = 0;

  map<ll, ll> pref[n];
  //	pref[n-2] = a[n-1];
  for (int i = n - 2; i >= 0; i--) {
    pref[i] = lcm(pref[i + 1], a[i + 1], flag);
    if (flag)
      break;
  }
  if (flag == 0)
    for (int i = 0; i < n; i++) {
      if (gcd(pref[i], a[i]))
        flag = 1;
    }

  if (flag == 0) {
    cout << "pairwise coprime";
  } else if (g == 1) {
    cout << "setwise coprime";
  } else {
    cout << "not coprime";
  }

  return 0;
}