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

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#pragma GCC optimize("unroll-loops,no-stack-protector")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("O3")
#pragma GCC target("sse4")
using namespace __gnu_pbds;
using namespace std;
/*
int dx[]={-1,1,0,0};
int dy[]={0,0,-1,1};
*/
#define ull unsigned long long int
#define ll long long int
#define ld long double
#define MAX 1000000007
// 1e9+7
#define pll pair<ll, ll>
#define pii pair<int, int>
#define piii pair<int, pii>
#define IO                                                                     \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define FO                                                                     \\
  ifstream fin("a.in");                                                        \\
  ofstream fout("a.out");
#define TEST                                                                   \\
  int testcase;                                                                \\
  cin >> testcase;                                                             \\
  while (testcase--)
#define FTEST                                                                  \\
  int testcase;                                                                \\
  fin >> testcase;                                                             \\
  while (testcase--)
#define all(x) (x).begin(), (x).end()
#define mset0(x) memset((x), 0, sizeof((x)));
#define mset1(x) memset((x), -1, sizeof((x)));
#define endl "\\n"
#define plll pair<ll, pll>
#define plii pair<ll, pii>
#define no                                                                     \\
  {                                                                            \\
    cout << "no" << endl;                                                      \\
    return;                                                                    \\
  }
#define yes                                                                    \\
  { cout << "yes" << endl; }
#define rep(i, N) for (int i = 0; i < N; i++)
const int maxn = 1e6;
const int mod = 998244353;
typedef tree<ll, null_type, less<ll>, rb_tree_tag,
             tree_order_statistics_node_update>
    indexed_set;
typedef tree<ll, null_type, less_equal<ll>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;

int arr[maxn + 1];
ll dp[maxn + 1];
int n;
int Max = 1;
bool isprime[maxn + 1];
int cnt[maxn + 1] = {0};
vector<int> prime;
int lpf[maxn + 1] = {0};
void sieve() {
  lpf[1] = 1;
  for (int p = 2; p <= Max; p++) {
    if (lpf[p] == 0) {
      lpf[p] = p;
      for (int q = p + p; q <= Max; q += p) {
        if (!lpf[q])
          lpf[q] = p;
      }
    }
  }
  /*
  for(int p=2;p<=Max;p++){
      cout<<p<<" "<<lpf[p]<<endl;
  }
  */
  return;
}
bool p() {

  sieve();
  int l, a;
  for (int i = 1; i <= n; i++) {
    a = arr[i];
    while (a != 1) {
      l = lpf[a];
      while (a % l == 0)
        a /= l;
      if (cnt[l] && l != 1)
        return (false);
      cnt[l]++;
    }
  }

  return (true);
}
bool s() {
  int g = arr[1];
  for (int i = 1; i <= n; i++) {
    g = __gcd(g, arr[i]);
    if (g == 1)
      return (true);
  }
  return (false);
}
void solve() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> arr[i];
    Max = max(Max, arr[i]);
  }
  if (p()) {
    cout << "pairwise coprime";

  } else if (s()) {
    cout << "setwise coprime";
  } else {
    cout << "not coprime";
  }
  return;
}
int main() {
  IO
  // TEST
  solve();
  return (0);
}
/*
2
3 3
2500 2500 2520
10 -5 -20
10 15 20
-15 17 13
2 3
2125 2098
-20 10 -10
10 10 -20
*/
/*
1
3 3
2500 2500 2520
10 -5 -20
10 15 20
-15 17 13

*/


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#pragma GCC optimize("unroll-loops,no-stack-protector")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("O3")
#pragma GCC target("sse4")
using namespace __gnu_pbds;
using namespace std;
/*
int dx[]={-1,1,0,0};
int dy[]={0,0,-1,1};
*/
#define ull unsigned long long int
#define ll long long int
#define ld long double
#define MAX 1000000007
// 1e9+7
#define pll pair<ll, ll>
#define pii pair<int, int>
#define piii pair<int, pii>
#define IO                                                                     \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define FO                                                                     \\
  ifstream fin("a.in");                                                        \\
  ofstream fout("a.out");
#define TEST                                                                   \\
  int testcase;                                                                \\
  cin >> testcase;                                                             \\
  while (testcase--)
#define FTEST                                                                  \\
  int testcase;                                                                \\
  fin >> testcase;                                                             \\
  while (testcase--)
#define all(x) (x).begin(), (x).end()
#define mset0(x) memset((x), 0, sizeof((x)));
#define mset1(x) memset((x), -1, sizeof((x)));
#define endl "\\n"
#define plll pair<ll, pll>
#define plii pair<ll, pii>
#define no                                                                     \\
  {                                                                            \\
    cout << "no" << endl;                                                      \\
    return;                                                                    \\
  }
#define yes                                                                    \\
  { cout << "yes" << endl; }
#define rep(i, N) for (int i = 0; i < N; i++)
const int maxn = 1e6;
const int mod = 998244353;
typedef tree<ll, null_type, less<ll>, rb_tree_tag,
             tree_order_statistics_node_update>
    indexed_set;
typedef tree<ll, null_type, less_equal<ll>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;

int arr[maxn + 1];
ll dp[maxn + 1];
int n;
int Max = 1;
bool isprime[maxn + 1];
int cnt[maxn + 1] = {0};
vector<int> prime;
void sieve() {
  memset(isprime, true, sizeof isprime);
  for (int p = 2; p * p <= Max; p++) {
    if (isprime[p]) {
      for (int q = p + p; q <= Max; q += p)
        isprime[q] = false;
    }
  }
  for (int p = 2; p <= Max; p++)
    if (isprime[p])
      prime.push_back(p);
}
bool p() {

  sieve();
  for (int i = 1; i <= n; i++) {

    for (auto p : prime) {
      // cout<<p<<endl;
      if (p > arr[i])
        break;
      if (arr[i] % p == 0)
        cnt[p]++;
      if (cnt[p] >= 2)
        return (false);
    }
  }
  return (true);
}
bool s() {
  int g = arr[1];
  for (int i = 1; i <= n; i++) {
    g = __gcd(g, arr[i]);
    if (g == 1)
      return (true);
  }
  return (false);
}
void solve() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> arr[i];
    Max = max(Max, arr[i]);
  }
  if (p()) {
    cout << "pairwise coprime";

  } else if (s()) {
    cout << "setwise coprime";
  } else {
    cout << "not coprime";
  }
  return;
}
int main() {
  IO
  // TEST
  solve();
  return (0);
}
/*
2
3 3
2500 2500 2520
10 -5 -20
10 15 20
-15 17 13
2 3
2125 2098
-20 10 -10
10 10 -20
*/
/*
1
3 3
2500 2500 2520
10 -5 -20
10 15 20
-15 17 13

*/