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

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define speedrap                                                               \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
int be(int a, int b, int mod) {
  if (b == 0)
    return 1;
  if (b % 2 == 1)
    return (a * be(a, b - 1, mod)) % mod;
  else
    return (be(a, b / 2, mod) * be(a, b / 2, mod)) % mod;
}
const int mod = 1e9 + 7;
#define PI 3.14159265359
#define pf push_front
#define pb push_back
#define bitcount __builtin_popcountll
#define setbitcnt __builtin_ctz
#define sc(a) scanf("%d", &a);
#define sz(s) (int)s.size()
#define eb emplace_back
#define pf push_front
#define mkp(x, y) make_pair(x, y)
#define all(x) x.begin(), x.end()
#define INF 0x3f3f3f3f
#define roundup(a, b)                                                          \\
  std::cout << std::fixed;                                                     \\
  std::cout << std::setprecision(b);                                           \\
  std::cout << a
const int siz = (1e6 + 10);
const int logi = 21;
const double epsilon = 1e-6;
bool cmp(const int &a, const int &b) { return (abs(a - b) > 1); }

bool prime[siz];
map<int, int> mp;
vector<int> vtp;

void sieve() {
  prime[0] = prime[1] = true;
  for (int i = 2; i < siz; i++) {
    if (prime[i] == false) {
      vtp.push_back(i);
      for (int j = 2 * i; j < siz; j += i) {
        prime[j] = true;
      }
    }
  }
}

bool pairwise_coprime() {
  for (int i = 0; i < vtp.size(); i++) {
    int cnt = 0;
    int val = vtp[i];
    if (prime[val] == true) {
      continue;
    }

    for (int j = val; j <= 1e6; j += val) {
      cnt += mp[j];
    }

    if (cnt > 1) {
      return false;
    }
  }

  return true;
}

signed main() {
#ifndef ONLINE_JUDGE
  freopen("in.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
#else
  // // online submission
#endif
  speedrap int t;
  t = 1;
  // cin>>t;
  int val = 0;
  while (t--) {
    int n;
    cin >> n;
    int a[n];
    sieve();
    for (int i = 0; i < n; i++) {
      cin >> a[i];
      mp[a[i]]++;
    }

    bool setwise = false;

    int g = 0;
    for (int i = 0; i < n; i++) {
      g = __gcd(g, a[i]);
    }

    if (g == 1) {
      setwise = true;
    }
    bool pairwise = pairwise_coprime();
    if (!pairwise and setwise) {
      cout << "setwise coprime";
      continue;
    }

    if (pairwise) {
      cout << "pairwise coprime";
      continue;
    }

    cout << "not coprime";
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define speedrap                                                               \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
int be(int a, int b, int mod) {
  if (b == 0)
    return 1;
  if (b % 2 == 1)
    return (a * be(a, b - 1, mod)) % mod;
  else
    return (be(a, b / 2, mod) * be(a, b / 2, mod)) % mod;
}
const int mod = 1e9 + 7;
#define PI 3.14159265359
#define pf push_front
#define pb push_back
#define bitcount __builtin_popcountll
#define setbitcnt __builtin_ctz
#define sc(a) scanf("%d", &a);
#define sz(s) (int)s.size()
#define eb emplace_back
#define pf push_front
#define mkp(x, y) make_pair(x, y)
#define all(x) x.begin(), x.end()
#define INF 0x3f3f3f3f
#define roundup(a, b)                                                          \\
  std::cout << std::fixed;                                                     \\
  std::cout << std::setprecision(b);                                           \\
  std::cout << a
const int siz = (1e6 + 10);
const int logi = 21;
const double epsilon = 1e-6;
bool cmp(const int &a, const int &b) { return (abs(a - b) > 1); }

map<int, int> mp;

bool pairwise_coprime() {
  for (int i = 2; i <= 1e6; i++) {
    int cnt = 0;

    for (int j = i; j <= 1e6; j += i) {
      cnt += mp[j];
    }

    if (cnt > 1) {
      return false;
    }
  }

  return true;
}

signed main() {
#ifndef ONLINE_JUDGE
  freopen("in.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
#else
  // // online submission
#endif
  speedrap int t;
  t = 1;
  // cin>>t;
  int val = 0;
  while (t--) {
    int n;
    cin >> n;
    int a[n];

    for (int i = 0; i < n; i++) {
      cin >> a[i];
      mp[a[i]]++;
    }

    bool setwise = false;

    int g = 0;
    for (int i = 0; i < n; i++) {
      g = __gcd(g, a[i]);
    }

    if (g == 1) {
      setwise = true;
    }

    if (!pairwise_coprime() and setwise) {
      cout << "setwise coprime";
      continue;
    }

    if (pairwise_coprime()) {
      cout << "pairwise coprime";
      continue;
    }

    cout << "not coprime";
  }
}