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

#include <bits/stdc++.h>
#define pb push_back
#define fo(i, n) for (int i = 0; i < n; i++)
#define ll long long
#define int long long
#define Mod 1000000007
#define MAX 4294967296 // 2^32
const int M = 1e9 + 7;
#define ip pair<int, int>
#define test(t)                                                                \\
  ll t;                                                                        \\
  cin >> t;                                                                    \\
  while (t--)

using namespace std;

long long gcd(int x, int y) {
  if (y > x)
    swap(x, y);
  if (y == 0)
    return x;
  return gcd(y, x % y);
}

void solve() {
  int n;
  cin >> n;
  int a[n];
  int count = 0;
  fo(i, n) { cin >> a[i]; }
  int ans = a[0];
  for (int i = 1; i < n; i++) {

    if (a[i] == 1 || ans == 1)
      break;
    ans = gcd(ans, a[i]);
  }

  if (ans > 1) {
    cout << "not coprime";
    return;
  }

  map<int, int> mp;

  fo(i, n) {
    int t = a[i];
    if (t == 1) {

      continue;
    }
    if (t % 2 == 0) {
      mp[2]++;
      while (t % 2 == 0 && t != 1)
        t /= 2;
    }
    for (int j = 3; j <= sqrt(t); j += 2) {
      if (t % j == 0) {
        mp[j]++;
        while (t % j == 0 && t != 1)
          t /= j;
        if (t == 1)
          break;
      }
    }
    if (t > 2)
      mp[t]++;
  }
  for (auto it = mp.begin(); it != mp.end(); it++) {
    if (it->second > (int)1) {
      cout << "setwise coprime";
      return;
    }
  }
  cout << "pairwise coprime";
}

signed main() {

  // std::string binary = std::bitset<8>(n).to_string();
  ios::sync_with_stdio(0);
  cin.tie(0);
  solve();
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define pb push_back
#define fo(i, n) for (int i = 0; i < n; i++)
#define ll long long
#define int long long
#define Mod 1000000007
#define MAX 4294967296 // 2^32
const int M = 1e9 + 7;
#define ip pair<int, int>
#define test(t)                                                                \\
  ll t;                                                                        \\
  cin >> t;                                                                    \\
  while (t--)

using namespace std;

long long gcd(int x, int y) {
  if (y > x)
    swap(x, y);
  if (y == 0)
    return x;
  return gcd(y, x % y);
}

void solve() {
  int n;
  cin >> n;
  int a[n];
  int count = 0;
  fo(i, n) { cin >> a[i]; }
  int ans = a[0];
  for (int i = 1; i < n; i++) {

    if (a[i] == 1 || ans == 1)
      break;
    ans = gcd(ans, a[i]);
  }

  if (ans > 1) {
    cout << "not coprime";
    return;
  }

  map<int, int> mp;

  fo(i, n) {
    int t = a[i];
    if (t == 1) {

      continue;
    }
    if (t % 2 == 0) {
      mp[2]++;
      while (t % 2 == 0 && t != 1)
        t /= 2;
    }
    for (int j = 3; j <= sqrt(a[i]); j += 2) {
      if (t % j == 0) {
        mp[j]++;
        while (t % j == 0 && t != 1)
          t /= j;
        if (t == 1)
          break;
      }
    }
    if (t > 2)
      mp[t]++;
  }
  for (auto it = mp.begin(); it != mp.end(); it++) {
    if (it->second > (int)1) {
      cout << "setwise coprime";
      return;
    }
  }
  cout << "pairwise coprime";
}

signed main() {

  // std::string binary = std::bitset<8>(n).to_string();
  ios::sync_with_stdio(0);
  cin.tie(0);
  solve();
}